Another week, another Riddler. This problem:

*You play a game with four balls: One ball is red, one is blue, one is green and one is yellow. They are placed in a box. You draw a ball out of the box at random and note its color. Without replacing the first ball, you draw a second ball and then paint it to match the color of the first. Replace both balls, and repeat the process. The game ends when all four balls have become the same color. What is the expected number of turns to finish the game?*

This one revolves around finding the expected number of turns required to get from your starting configuration (all different colors) to the ending configuration (all the same). To do this we can set up a system of equations:

##### The 3 Ball Case:

We will be using the following notation: where represents the number of colors with one ball representing them and represents the quantity of balls. So in the 3 ball case we start with because each of the three colors have one ball. One ball will be painted the same as another, giving you . At this point you have a 2/3rds chance to use a whole turn that leaves you in a (in essence calling itself) and a 1/3rds chance to get to the final . To find the average number of turns spent in this loop I used:

therefore:

because it takes one turn to get the to A case.

#### The 4 Ball Case:

A similar set of equations can be set up:

solving this gives us

This can be expanded to show the general equation to be:

##### Experimentally

This was done in Python as well to show the distribution:

import random import copy import matplotlib.pyplot as plt num_balls = 4 iterations = 1000000 standard_array = [] for _ in range(0,num_balls): standard_array.append(_) def game(array): working_array = copy.deepcopy(array) turns = 0 while len(set(working_array))&gt;1: rand_1 = 0 rand_2 = 0 while rand_1 == rand_2: rand_1 = random.randint(0,len(array)-1) rand_2 = random.randint(0,len(array)-1) working_array[rand_2] = working_array[rand_1] turns = turns + 1 return turns total_turns = 0 results = [] for i in range(0,iterations): val = game(standard_array) total_turns = total_turns + val results.append(val) print("Average # of turns: " + str(float(total_turns/iterations))) num_bins = len(set(results)) n, bins, patches = plt.hist(results, bins=list(set(results)), normed=1, facecolor='green', alpha=0.5) plt.ylabel("Frequency") plt.xlabel("Number of Turns") plt.title("A Painting Puzzle with " + str(num_balls) + " balls\n in " + str(iterations) + " iterations") plt.show()