I was in a class recently, and was asked the following question:
Imagine you have a hat containing pieces of paper numbered from $1$ to $N$. You remove two pieces of paper at random from the hat, and replace them with the absolute value of the difference between the two numbers. You repeat this process until there is one piece of paper remaining. What can you tell about the final piece of paper?
When we draw two pieces of paper, we either reduce the amount of odd numbers by two (if two odd numbers are drawn, resulting in an even number), or the amount of odd numbers stays the same. Thus we can deduce that the final piece of paper will be even if the starting amount of odd numbers is even, and odd if the starting amount of odd numbers is odd.
As an extension to this, I wondered what the probability distribution of the final piece of paper in the hat is, i.e. for $X$ being the random variable representing the final piece of paper in the hat, what is $P_N(X = k)$, for $k \in \{0, 1, ..., N\}$?
In order to find some sort of pattern, I wrote the following Python program to simulate the game for any N, and return an array of the amount of times each number was left in the hat.
# Import necessary dependencies import numpy as np import matplotlib.pyplot as plt from tqdm import tqdm def finalPiece(n): ''' Pick pieces out of the hat randomly, replace with the absolute value of the difference and return the final number left. ''' numberOfPieces = n piecesInHat = list(range(1, n+1)) while numberOfPieces > 1: # Pick random piece of paper choice1Index = np.random.randint(0, numberOfPieces) choice2Index = np.random.randint(0, numberOfPieces-1) # Remove pieces of paper from hat choice1 = piecesInHat.pop(choice1Index) choice2 = piecesInHat.pop(choice2Index) # Replace with new number piecesInHat.append(abs(choice1-choice2)) numberOfPieces = numberOfPieces - 1 return piecesInHat[0] def experiment(numbersInHat, numberOfTrials, plot=False, save=False): ''' Repeat the finalPiece function and count how many times each number is left in the hat. Plot the result if plot == True. Save the results array if save == True. ''' results = np.zeros(numbersInHat+1, dtype=int) # Count number of times each number is left in the hat, with progress bar for _ in tqdm(range(numberOfTrials)): results[finalPiece(numbersInHat)] += 1 # Make a plot if it is desired if plot: x = np.linspace(0, numbersInHat, numbersInHat+1, dtype=int) plt.figure(figsize=(8, 6), dpi=800) plt.xlabel('Final Number in the Hat') plt.ylabel('Percentage of Experiments') plt.title('Hat Numbers Experiment: ' + str(numbersInHat) + ', ' + str(numberOfTrials)) plt.bar(x, results*100/numberOfTrials) plt.savefig('bar graph ' + str(numbersInHat) + ' ' + str(numberOfTrials) + '.png') #plt.show() # Save results to file if it is desired if save: np.savetxt('counts ' + str(numbersInHat) + ' ' + str(numberOfTrials) +'.txt', results, fmt='%d') # Return results array (counts of experiments) return results This shows the probability decreasing as $k$ increases (with $k$ of appropriate parity, and $k\neq 0$), but I still haven't been able to work out what the distribution actually is. Any assistance would be much appreciated.
Edit: To clarify, I am seeking an explicit formula for $P_N(X = k)$ if possible. Using the code above, I have already explored the distribution stochastically for large $N$.