# Optimal Die for Straight Path

A friend of mine posed an interesting question: given a game where you are  spaces away from a target space on an infinite linear sequence of tiles, what is the optimal choice of -sided die to use for movement such that you minimize the average number of turns it takes to get to your goal?

Above is the problem setup. You have an infinite line of spaces ranging from  to , with the origin as your goal. You start at space . You may now choose a -sided die (or, rather, a number generator that returns an integer  from a uniform distribtuion). Every turn you roll your die and move a number of spaces towards the origin equal to what you rolled.

Consider the case . You are two spaces away from the origin and want to get there in as few turns as possible. Suppose you choose to use a 2-sided die . On your first turn you can either roll a 1 or a 2. If you roll a 2, you end up at the origin, and arrived there in 1 turn.

If you roll a 1, however, you end up on the 1 space. On your next turn you either roll a 1 and reach the origin or roll a 2 and end up on the -1 space. Above you can see the possible sequence of moves. In the worst possible case you cycle between  forever, but odds are eventually you reach the origin.

What are the average number of turns to reach the origin for ?

Well, since the die is perfect, you get the following:







So, if you are 2 spaces away and you choose to use a 2-sided die, the average number of turns is 2. Interesting.

Also note that the case  is the same. Here you are 1 space away, and it also takes you on average 2 turns to get to the origin. This is because being at state 2, 1, and -1 are all the same, turn-wise from the origin when wielding a 2-sided die.

Let us take a look at the case .





And we again notice that the spaces 3,2,1,-1,-2 are all the same when wielding a 3-sided die, so .

Is there a pattern here? Yes.

.

And, more interestingly, being within  spaces with a -sided die causes you to be on average  turns away from your goal.

.

But what if you have a -sided die and are further than  spaces from the origin? What if you have a 2-sided die and are 3 spaces from the origin?

Let's work it out: 

Similarly: 

In fact: .

In the general case, we get:

.

Thus, we can easily generate the average number of turns for any pairwise value . But, we still haven't answered the question. What is the optimal choice for N?

To answer this, let us look at the solution for :

We find that the solution is not actually , though that would have been a good guess. For , the optimal choice is not 4, but 5. Actually,  has an optimal choice of 5 as well.

So there you have it. We didn't find a closed-form solution but we can easily compute several candidate cases and find the optimal choice.