Consider the sequence of
positive integers
1
,
2
,
3
,
4
,
5
,
6
,
…
. An operation
throw
consists of taking the first number and move it to the back of the
(
k
+
1
)
th
number where
k
is the second number in the sequence. For example,
1 2 3 4 5 6 |
|
The first occurrence of 2, 3, 4, 5 being the head of the sequence are after 1, 2, 5 and 12 throws, respectively. How many
throw
operations do you need to perform to get 20 in the head of the sequence?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Great identifying these 2 lemmas to help the entire presentation.
Minor points:
1. Better control/definition of the variables would be helpful. For example, in Lemma 1 induction hypothesis,
a
is used both as a variable in the sequence, and as a variable for the iterated inductions. This is one reason why the induction case is done from
P
k
to
P
k
+
1
, while the statement is just
P
n
.
2. Explain what the actual classification should be. For example, in Lemma 2, saying "It takes X moves to bring b to the k-th position" is vauge. Is this the very first time that b is in the k-th positon? Or, is the important thing to describe exactly what the Xth move looks like?
3. For the result, better presentation (starting a new line / paragraph) will make it easier to understand what you are saying.
I have found the relation that the number of throws require to get n to the head of the sequence is:
2 n − 1 − ( n − 1 ) n = 1
I've not been able to prove this yet but it seems to hold for all n = 1 . This gives the answer as:
2 2 0 − 1 − ( 2 0 − 1 ) = 5 2 4 2 6 9
Here's a solution in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
and an arguably easier to read solution in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Problem Loading...
Note Loading...
Set Loading...
Of course there's the boring solution:
But it's boring. Besides, what if I ask the number of throws required to bring 2 0 1 6 2 0 1 6 to the front of the sequence?
Let's solve this without coding at all.
Lemma 1 : Given a sequence with initial terms 1 , 2 , 3 , … , k − 1 , k , a , t k + 2 , t k + 3 , … , t a + 1 , … , after 2 k − 1 moves, the sequence will look like 1 , 2 , 3 , … , k − 1 , a , t k + 2 , t k + 3 , … , t a + 1 , k , … . In other words, k is moved to the back of the ( a + 1 ) -th term.
The proof is by induction on k . (Note that a doesn't matter.) The base case k = 1 is trivial: the sequence looks like 1 , a , … , so a single move throws the 1 to the back of the ( a + 1 ) -th element. Suppose we have proven the claim for k ≤ k ′ , and we want to prove the claim for k = k ′ + 1 .
Consider the sequence 1 , 2 , 3 , … , k ′ − 2 , k ′ − 1 , k ′ , k ′ + 1 , a , t k + 2 , … . By inductive hypothesis with k ← k ′ , a ← k ′ + 1 , after 2 k ′ − 1 moves the sequence will look like 1 , 2 , 3 , … , k ′ − 2 , k ′ − 1 , k ′ + 1 , a , k ′ , t k + 2 , … . Invoking it again now with k ← k ′ − 1 , a ← k ′ + 1 , after 2 k ′ − 1 − 1 moves the sequence becomes 1 , 2 , 3 , … , k ′ − 2 , k ′ + 1 , a , k ′ , k ′ − 1 , t k + 2 , … . We can keep going, invoking the inductive hypothesis for k ← k ′ , k ′ − 1 , k ′ − 2 , … , 1 and a ← k ′ + 1 in turn, so that after ( 2 k ′ − 1 ) + ( 2 k ′ − 1 − 1 ) + … + ( 2 1 − 1 ) = 2 k ′ + 1 − k ′ − 2 moves, the sequence looks like k ′ + 1 , a , k ′ , k ′ − 1 , k ′ − 2 , … , 1 , t k + 2 , … .
Now, a single move throws k ′ + 1 to the back of the ( a + 1 ) -th term. But we still have the front of the sequence looking like a , k ′ , k ′ − 1 , k ′ − 2 , … , 1 , t k + 2 , … . So, we apply k ′ more moves; the first sets a to be the ( k ′ + 1 ) -th term, the second sets k ′ to be the k ′ -th term, the third sets k ′ − 1 to be the ( k ′ − 1 ) -th term, and so on, until the last move sets 2 to be the second term; now the 1 is also the first term. The sequence now looks 1 , 2 , 3 , … , k ′ , a , t k + 2 , … , t a + 1 , k ′ + 1 , … . That's a total of ( 2 k ′ + 1 − k ′ − 2 ) + 1 + k ′ = 2 k ′ + 1 − 1 moves, proving the claim for k = k ′ + 1 . By induction, the claim is proven for all k .
Lemma 2 : Given a sequence with initial terms 1 , 2 , 3 , … , k − 1 , a , b , t k + 2 , t k + 3 , … , t a + 1 , … , it takes 2 k − 1 − ( k − 1 ) moves to bring b to the k -th position, and the resulting sequence is a , k − 2 , k − 3 , … , 1 , b , t k + 2 , t k + 3 , … , t a + 1 , k − 1 , … .
Proving that 2 k − 1 − ( k − 1 ) moves is enough is easy. We invoke Lemma 1 with k ← k − 2 , k − 1 , … , 1 in order and a ← k − 1 (this is also part of the proof in Lemma 1). We obtain the sequence k − 1 , a , k − 2 , k − 3 , … , 1 , b , … . One additional move throws k − 1 to after the ( a + 1 ) -th element; since a ≥ k , this is after b , thus showing that we manage to bring b to the k -th position. It takes ( 2 k − 2 − 1 ) + ( 2 k − 3 − 1 ) + … + ( 2 1 − 1 ) + 1 = 2 k − 1 − ( k − 1 ) moves to do so.
Proving it is necessary is harder. We prove this by induction on k . For k = 2 , this is trivial; the sequence looks like 1 , a , b , t 4 , t 5 , … , t a + 1 , … , and a single move makes it a , b , t 4 , t 5 , … , t a + 1 , 1 , … , proving both parts of the claim at once. Suppose we have proven the claim for k < k ′ , and we want to prove the claim for k = k ′ .
The only way we can bring b down to k ′ -th position is if we throw something behind it. This means, we need to make the second element to be k ′ or greater (since b is in the k ′ + 1 -th position). However, this element must also be found in front of b ; if it is behind b , then to bring it down, we also need to bring b down, and thus it is circular logic. The only element in front of b that is at least k ′ is a . Thus we need to bring a to the second position.
By inductive hypothesis on k ← k ′ − 2 , a ← k ′ − 1 , b ← a , we obtain the sequence k ′ − 1 , k ′ − 3 , k ′ − 4 , k ′ − 5 , … , 1 , a , k ′ − 2 , b , … after 2 k ′ − 2 − ( k ′ − 2 ) moves. Doing k ′ − 3 additional moves, the sequence is "fixed", making 1 , 2 , 3 , … , k ′ − 4 , k ′ − 3 , k ′ − 1 , a , k ′ − 2 , b , … . This is a total of 2 k ′ − 2 − 1 moves required. (Note that Lemma 1 says this is the sequence is the result after 2 k ′ − 2 − 1 moves, but doesn't say whether we can reach it earlier.) We repeat this with k ← k ′ − 3 , a ← k ′ − 1 , b ← a ; this is 2 k ′ − 3 − 1 additional moves to make it 1 , 2 , 3 , … , k ′ − 4 , k ′ − 1 , a , k ′ − 2 , k ′ − 3 , b , … . Once again, we do this for k ← k ′ − 2 , k ′ − 3 , … , 1 in order, a ← k ′ − 1 , b ← a ; this leads to the sequence k ′ − 1 , a , k ′ − 2 , k ′ − 3 , … , 1 , b , … in ( 2 k ′ − 2 − 1 ) + ( 2 k ′ − 3 − 1 ) + … + ( 2 1 − 1 ) = 2 k ′ − 1 − k ′ moves, and now we achieved our aim. This is also the smallest necessary, since we invoked the inductive hypothesis which says this is necessary. (Lemma 1 only says this is sufficient.) One final move completes the job, proving the inductive hypothesis for k = k ′ , and thus by induction, Lemma 2 is proven for all k .
With Lemma 2 proven, we will now prove the main result:
Result : It takes 2 n − 1 − ( n − 1 ) moves to bring n to the first element of the sequence.
We repeat the proof in Lemma 2.
Invoking Lemma 2 for k ← n − 2 , a ← n − 1 , b ← n gives 2 n − 2 − ( n − 2 ) moves to obtain the sequence n − 1 , n − 3 , n − 4 , … , 1 , n , n − 2 , … . Doing n − 3 moves moves makes the sequence 1 , 2 , 3 , … , n − 3 , n − 1 , n , n − 2 , … in 2 n − 2 − 1 moves.
We repeat it for k ← n − 3 , then for k ← n − 4 , and so on until k ← 1 , to obtain the sequence n − 1 , n , n − 2 , n − 3 , n − 4 , … , 1 within ( 2 n − 2 − 1 ) + ( 2 n − 3 − 1 ) + … + ( 2 1 − 1 ) = 2 n − 1 − n moves; one final move throws n − 1 away and makes n at the front of the sequence, done in 2 n − 1 − ( n − 1 ) moves.
Due to the result in Lemma 2, we know that there is no faster method to bring n to the ( n − 1 ) -th position other than k ← n − 2 , a ← n − 1 , b ← n ; there is no faster method to bring n to the ( n − 2 ) -th position other than k ← n − 3 , a ← n − 1 , b ← n ; and so on, so this is indeed the shortest way to bring n to the front of the sequence. This proves that it takes 2 n − 1 − ( n − 1 ) moves to do so.
I'm sure this can be simplified, with so many repeated statements, but I can't think of it at the moment.