Write down all the integers 1 to 1,000,000 in a row.
Let A be this last number standing.
Now, write down the numbers 1 to one million again. Repeat the process, but this time with the words "even" and "odd" switched. Let B be the last number remaining in this case.
What is the sum A + B ?
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.
Fantastic solution!
Log in to reply
Thank you :)
@Zain Majumder You may enjoy this problem
First, observe that in both cases, after the n th elimination step the remaining numbers form an arithmetic sequence with common difference 2 n . Also, after each elimination step the first term of the new sequence is alternately the first or second term of the old sequence. Let a i n be the i th term in the n th iteration of the first sequence, and similarly for b i n for the second sequence. Consider the first two terms in the first few iterations of each sequence:
n | { a i n } | { b i n } |
1 | { 2 , 4 , … } | { 1 , 3 , … } |
2 | { 2 , 6 , … } | { 3 , 7 , … } |
3 | { 6 , 1 4 , … } | { 3 , 1 1 , … } |
4 | { 6 , 2 2 , … } | { 1 1 , 2 7 , … } |
5 | { 2 2 , 5 4 , … } | { 1 1 , 4 3 , … } |
6 | { 2 2 , 8 6 , … } | { 4 3 , 1 0 7 , … } |
If we continue the iteration until the first term of each sequence is less than 1 million, and every term after the first is greater than 1 million, then those first terms are A and B . Let { A k } be the sequence of first terms of the first sequence, ignoring repetition, and define { B k } similarly for the second sequence. We can see that A k = 2 + 4 + 1 6 + 6 4 + ⋯ + 4 k − 1 = 1 + ∑ i = 0 k 4 i = 1 + 4 − 1 4 k − 1 = 3 4 k + 2
and
B k = 1 + 2 + 8 + 3 2 + ⋯ + 2 ⋅ 4 k − 1 = 1 + 2 ∑ i = 0 k 4 i = 1 + 2 4 − 1 4 k − 1 = 3 2 ⋅ 4 k + 1 .
Solving both A k < 1 , 0 0 0 , 0 0 0 and B k < 1 , 0 0 0 , 0 0 0 gives k ≤ 1 0 , so A + B = A 1 0 + B 1 0 = 3 4 9 , 5 2 6 + 6 9 9 , 0 5 1 = 1 , 0 4 8 , 5 7 7 .
but how to strictly prove the remaining numbers form such property?
This sum is not correct, 349526+699051=1048577, not 1048477
My solution is same as yours. 👍
using M a t h e m a t i c a
s=Range@1000000;
n=1;
While[Length@s>1,s=Drop[s,{1+Abs@Sin[n*Pi/2],-1,2}];n++];
s
this code does the thing and you only have to switch n
n=1 returns 699051
n=2 returns 349526
result 1048577
this is not a complete proof, just some good intuition
Imagine instead of writing the numbers from 1 to 1 0 0 0 0 0 , you write the numbers from 1 to 1 0 4 8 5 7 6 , which is 2 2 0 . By eliminating all the odd numbers, you eliminate 1 , and you leave 1 0 4 8 5 7 6 , which is the last number, and by eliminating the evens, you eliminate the last, but leave the first. This means doing the reversed operation is the same as doing the normal operation, but reading the list backwards, we know it will never get to a point where both the first and last numbers get eliminated because we chose a power of 2 as our list length. This means if by doing the regular operation, the last number remaining is n , by doing reversed operation, the last number remaining is ( 1 0 4 8 5 7 6 + 1 − n ) , which is the n t h number, if you start reading backwards from the last number. Therefore, the sum is 1 0 4 8 5 7 6 + 1 − n + n = 1 0 4 8 5 7 7
To make this rigorous, the main thing you'd have to prove that I didnt explain here is that neither the normal operation, neither the reversed leave a number between 1 0 0 0 0 0 0 and 1 0 4 8 5 7 7 as the last non-removed number.
Great intuition dude.Actually I solved the problem just now and wanted to see how others had solved it . Some have written here just about their observations without intuitively explaining why the pattern exists,One guy KT hints the reason for the irregularity in the pattern ,while some others write a damn code which pissed me off!How the hell will that help us ,Please guys don't write the codes which are not executable by ur brain . And coming to ur solution we can prove that numbers from 1-48577 will vanish by the end of the process(either of the two filtering) which will also mean that no number from 1000000-1048577 survives from ur argument. The reason being the number of numbers in the set {1,2,...48577} keeps becoming half after each time the operation is done and by the end of around 17th or 18th(just because 2^16 is 65536 and extra steps for making that one element vanish after atmost two more steps ) time filtering no elements from 1-48577 remain while still some elements which are not in the extremes like {48578-1000000} will exist . I may have not explained this clearly but ur intuition was awesome.
Did the lazy man's approach and coded a program to solve it.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
By using Python3's powerful range object, the result is almost instant.
1 2 3 4 5 6 7 8 9 10 11 |
|
Edited according to Paul Evans' suggestion.
+1 Excellent use of ranges! You also could have used a[0] + b[0] to get the answer.
Write the numbers in binary, and you will see that each step fixes a bit to a value 0 or 1, starting at the least significant bit. In the beginning we get a minor irregularity (which has to do with starting with 1 rather than 0), but after that, the pattern alternates.
For A we get binary ...1010110 where the most significat bit has value 2 1 8 .
For B we get binary ...0101011 where the most significat bit has value 2 1 9 .
A and B add up to 2^20+1.
Heres some code, runs in like a second or so and prints the correct answer
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 |
|
with javascript (you can run this here in your browser, opening the javascript console), define:
our original list (array) of numbers
const oneToOneMillion = Array(1000000).fill(1).map((_, i) => i + 1)
the functions
even
and
odd
, used to get if the index is even or odd
const even = (_, i) => !(i % 2)
const odd = (_, i) => i % 2
the function that will filter the list until only one element remains
const continueFilter = (f1, f2, array) => array.length == 1 ? array[0] : continueFilter(f2, f1, array.filter(f1))
and now you get
continueFilter(even, odd, oneToOneMillion) + continueFilter(odd, even, oneToOneMillion)
=> 1048577
The numbers you cross out in each iteration are given by a sequence with general expression a*n-b, where a=2 in the first iteration, a=4 in the second, a=8 in the third, and a=2^k in the k-th iteration. Then you obtain b by subtracting the first odd or even element ot the sequence from a. Using this fact, I wrote the following code in Maple to obtain the result:
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 |
|
We let the operator E ^ cross out all the numbers in odd positions of an arbitrary given sequence { a n } , ( n > 0 ) , selecting only the numbers at even positions to remain. What this really means is that we're applying a change of variables n → 2 n . Similarly, we let the operator O ^ cross out all the numbers in even positions of a given sequence, selecting only the numbers at odd positions to remain. Again, this just means applying a change of variables n → 2 n − 1 .
Having defined these operators, we select the original sequence { c n } to be the list of consecutive positive numbers from 1 to 1,000,000. First, we take a look at the sequence { e n } that remains in the list during the k t h iteration I k if we start out by using the operator E ^ .
I 0 : { c n } = { n } I 1 : { e n } = E ^ { ( c n ) } = { ( 2 n ) } I 2 : { e n } = O ^ E ^ { ( c n ) } = { 2 ( 2 n − 1 ) } = { 4 n − 2 } I 3 : { e n } = E ^ O ^ E ^ { ( c n ) } = { 4 ( 2 n ) − 2 } = { 8 n − 2 } I 4 : { e n } = O ^ E ^ O ^ E ^ { ( c n ) } = { 1 6 n − 1 0 } I 5 : { e n } = E ^ O ^ E ^ O ^ E ^ { ( c n ) } = { 3 2 n − 1 0 }
Generally, I k : { e n } = . . . O ^ E ^ { ( c n ) } = 2 k n − 3 2 ( 4 ⌊ 2 k ⌋ − 1 )
(Although the range of n shrinks over each iteration, we won't pay meticulous attention to its range, because we have nothing to lose.)
Now for there to be only one item in the sequence { e n } , we must set the hypothetical second term to be greater than 1,000,000, that is: e 2 = 2 k + 1 − 3 2 ( 4 ⌊ 2 k ⌋ − 1 ) > 1 , 0 0 0 , 0 0 0
and choose the smallest k that satisfies this equation. This is found to be k = 2 0 . This means that it is during the 2 0 t h iteration of crossing out numbers that only one number will remain. This number is of course the first term in the sequence:
A = e 1 = 2 2 0 − 3 2 ( 4 1 0 − 1 )
Similarly, if we start out with the O ^ operator, the sequence { o n } that remains in the list during the k t h iteration I k will be given by:
I 0 : { c n } = { n } I 1 : { o n } = O ^ { ( c n ) } = { ( 2 n − 1 ) } I 2 : { o n } = E ^ O ^ { ( c n ) } = { 2 ( 2 n ) − 1 } = { 4 n − 1 } I 3 : { o n } = O ^ E ^ O ^ { ( c n ) } = { 8 n − 5 } I 4 : { o n } = E ^ O ^ E ^ O ^ { ( c n ) } = { 1 6 n − 5 } I 5 : { o n } = O ^ E ^ O ^ E ^ O ^ { ( c n ) } = { 3 2 n − 2 1 }
Generally, I k : { o n } = . . . E ^ O ^ { ( c n ) } = 2 k n − 3 1 ( 4 ⌈ 2 k ⌉ − 1 )
Again, for there to be only one item in the sequence { o n } , we must set the hypothetical second term to be greater than 1,000,000, that is: o 2 = 2 k + 1 − 3 1 ( 4 ⌈ 2 k ⌉ − 1 ) > 1 0 0 0 0 0 0 .
The smallest k that can satisfy this is once again k = 2 0 . Then, on the 20th iteration, only one number will remain:
B = o 1 = 2 2 0 − 3 1 ( 4 1 0 − 1 )
Then, A + B = 2 2 0 − 3 2 ( 4 1 0 − 1 ) + 2 2 0 − 3 1 ( 4 1 0 − 1 ) A + B = 2 2 0 + 2 2 0 − 3 3 ( 4 1 0 − 1 ) A + B = 2 2 0 + 2 2 0 − ( 4 1 0 − 1 ) A + B = 2 2 0 + 2 2 0 − ( 2 2 0 − 1 ) A + B = 2 2 0 + 1 = 1 , 0 4 8 , 5 7 7
Problem Loading...
Note Loading...
Set Loading...
I will first solve the problem if the list contained the numbers 0 to 999,999 instead of 1 to 1,000,000. Then I will show how to modify it to solve the original problem.
(a) Let the numbers 0 to 999,999 correspond to the indices of the positions of the numbers. Let us represent these as 20-bit strings (because 2 1 9 < 1 , 0 0 0 , 0 0 0 , < 2 2 0 ). Crossing out the numbers in the odd positions (i.e. with odd indices) corresponds to crossing to crossing out all the numbers whose least significant bit is 1.
Those with least significant bit 0 survive. In step ii we cross out all the numbers with even indices in the surviving list. This corresponds to crossing out all the numbers whose second least significant bit is 0. In the next step we cross out the odd indices from the remaining list, i.e. those whose third least significant bit is 1. and so on. Thus, the unique number that survives this process is the number whose index is represented by the 20-bit string 10101010101010101010. Because our list of numbers is 0 to 999,999, and in the initial list the number stored at index i is i , this means that the unique surviving number is the number whose binary representation is 10101010101010101010 In the second process, we do the same thing, only with odd and even switched. It easily follows that the unique surviving number in this process is the number whose binary representation is 01010101010101010101 (a 20 bit string with leading 0). Adding these numbers together gives the number with binary representation 11111111111111111111, or 2 2 0 − 1
(b) To go back to solving the original problem , note that when the list is 1 to 1,000,000 (but the indexing still starts at 0) the initial number at index i is i + 1 . However, the crossing out processes operated only on the indices, not on the contents of the list. Thus both processes result in the same indices surviving, so that the surviving numbers are 1 more than the corresponding surviving numbers in (a). Thus when the two are added up, the sum is 2 more than the answer in (a) or 2 2 0 + 1 = 1 , 0 4 8 , 5 7 7