A game of 2's

Sharky and Ivan are playing a strange game, with Sharky going first.

They start with a heap of 420 coins. They then take turns to remove some of the coins using the two rules below.

Given that the number of coins in the pile is k , k, you may either

  1. remove the largest power of 2 less than k , k, if k k is not a power of 2, or
  2. remove half the coins only if k k is even.

If a person cannot make a legal move, they lose. Who has the winning strategy?

Sharky Ivan

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.

8 solutions

Sharky Kesa
May 19, 2017

Reading the question, we find that this is a game. Obviously, the first thing everyone solving this should do is to

Play the game! \huge \text{Play the game!}

Let's try a few smaller values and see what we get. (The first column represents the player who has just taken coins, and n n represents the number of coins in the pile)

Player Operation n Start 22 Sharky 1 6 Ivan 2 3 Sharky 1 1 \begin{array}{c|c|c} \text{Player} & \text{Operation} & n\\ \hline \text{Start} & \text{ } & 22\\ \text{Sharky} & 1 & 6\\ \text{Ivan} & 2 & 3\\ \text{Sharky} & 1 & 1\\ \end{array}

Notice that Ivan can't play any operation on 1 since it is both a power of 2 and odd. We observe that the winner will make n = 1 n=1 . Thus, Sharky won if Ivan had used the first operation. Let's see what happens if Ivan had used the other operation.

Player Operation n Start 22 Sharky 1 6 Ivan 1 2 Sharky 2 1 \begin{array}{c|c|c} \text{Player} & \text{Operation} & n\\ \hline \text{Start} & \text{ } & 22\\ \text{Sharky} & 1 & 6\\ \text{Ivan} & 1 & 2\\ \text{Sharky} & 2 & 1\\ \end{array}

So it seems Sharky has the winning strategy for 22. Let's try more numbers!

Player Operation n Start 28 Sharky 1 12 Ivan 2 6 Sharky 2 3 Ivan 1 1 \begin{array}{c|c|c} \text{Player} & \text{Operation} & n\\ \hline \text{Start} & \text{ } & 28\\ \text{Sharky} & 1& 12\\ \text{Ivan} & 2 & 6\\ \text{Sharky} & 2 & 3\\ \text{Ivan} & 1 & 1\\ \end{array}

In this case, it seems Ivan won. Using our previous game, we know 6 is a losing position for the person who's about to remove coins from it. Thus, we don't need to investigate Sharky's second move. What about his first move though?

Player Operation n Start 28 Sharky 2 14 Ivan 1 6 Sharky 2 3 Ivan 1 1 \begin{array}{c|c|c} \text{Player} & \text{Operation} & n\\ \hline \text{Start} & \text{ } & 28\\ \text{Sharky} & 2 & 14\\ \text{Ivan} & 1 & 6\\ \text{Sharky} & 2 & 3\\ \text{Ivan} & 1 & 1\\ \end{array}

Here, we find almost the same position yet again! Thus, we know Ivan has a winning strategy for 28.

If you keep playing this for different values of n n , you might observe some sort of pattern. However, how would you prove it? Think about this, then keep reading down to find out a great strategy for solving this.


If you were thinking about converting n n to binary, you are correct in thinking so. Motivation for thinking about this is because you are either subtracting the greatest power of 2 from it, which translates in binary to removing the first digit 1 (if there exist other non-zero digits), or halving it, which translates in binary to removing the last digit 0 (if it exists). These both feel like easy operations to deal with. Let's analyse the above games with a conversion to binary to see what's happening.

Player Operation n n 2 Start 22 10110 Sharky 1 6 110 Ivan 2 3 11 Sharky 1 1 1 \begin{array}{c|c|c|c} \text{Player} & \text{Operation} & n & \overline{n}_2\\ \hline \text{Start} & \text{ } & 22 & 10110\\ \text{Sharky} & 1 & 6 & 110\\ \text{Ivan} & 2 & 3 & 11\\ \text{Sharky} & 1 & 1 & 1\\ \end{array}

Player Operation n n 2 Start 22 10110 Sharky 1 6 110 Ivan 2 2 10 Sharky 1 1 1 \begin{array}{c|c|c|c} \text{Player} & \text{Operation} & n & \overline{n}_2\\ \hline \text{Start} & \text{ } & 22 & 10110\\ \text{Sharky} & 1 & 6 & 110\\ \text{Ivan} & 2 & 2 & 10\\ \text{Sharky} & 1 & 1 & 1\\ \end{array}

Player Operation n n 2 Start 28 11100 Sharky 1 12 1100 Ivan 2 6 110 Sharky 2 3 11 Ivan 1 1 1 \begin{array}{c|c|c|c} \text{Player} & \text{Operation} & n & \overline{n}_2\\ \hline \text{Start} & \text{ } & 28 & 11100\\ \text{Sharky} & 1& 12 & 1100\\ \text{Ivan} & 2 & 6 & 110\\ \text{Sharky} & 2 & 3 & 11\\ \text{Ivan} & 1 & 1 & 1\\ \end{array}

Player Operation n n 2 Start 28 11100 Sharky 1 14 1110 Ivan 2 6 110 Sharky 2 3 11 Ivan 1 1 1 \begin{array}{c|c|c|c} \text{Player} & \text{Operation} & n & \overline{n}_2\\ \hline \text{Start} & \text{ } & 28 & 11100\\ \text{Sharky} & 1& 14 & 1110\\ \text{Ivan} & 2 & 6 & 110\\ \text{Sharky} & 2 & 3 & 11\\ \text{Ivan} & 1 & 1 & 1\\ \end{array}

From here, we can see that the first operation can only be applied a 1 a-1 times, where a a signifies the number of 1's in the binary representation of n n , and also that the second operation can be applied b b times, where b b signifies the number of terminating 0's in the binary representation of n n .

Observe that after each move, the sum a + b a+b decreases by 1. Thus, after both Sharky and Ivan have their turns, a + b a+b decreases by 2. Therefore, we must have a + b a+b constant in ( m o d 2 ) \pmod{2} after Sharky and Ivan have made their moves. This simplifies the problem dramatically. We also note from previously that if n = 1 n=1 , then the next player loses.

If a + b a+b is odd, then we must have a + b 1 ( m o d 2 ) a+b\equiv 1\pmod{2} . After Sharky and Ivan keep making their moves, eventually, Ivan will have made n n to be equal to 1. Thus, Ivan wins if a + b a+b is odd. Similarly, if a + b a+b is even, Sharky wins.

We have solved the general case!

To solve for n = 420 n=420 , we note n 2 = 110100100 \overline{n}_2 = 110100100 , which has a = 4 a=4 and b = 2 b=2 , so a + b a+b is even. Thus, Sharky is the winner.

And (as is often the case in games like this) there is no "strategy"--the winner is predetermined no matter how the players play.

Patrick Corn - 4 years ago

Log in to reply

In most of the games (including these), then yeah. Not all, fortunately.

Steven Jim - 4 years ago

Wow... Great solution!

Steven Jim - 4 years ago

Though I had solved it, but as soon as saw that "Sharky" gave the question, I knew that "Sharky" must be having the winning strategy

Nilaksh Singh - 4 years ago

Log in to reply

Oh, come on XD

Steven Jim - 4 years ago

Playing the game reveals that:

  • The first player wins when the starting number of coins is 2 2 , 3 3 , 5 5 , 8 8 , 9 9 , 12 12 , 14 14 , 15 15 , . . . ...

  • The first player loses when the starting number of coins is 1 1 , 4 4 , 6 6 , 7 7 , 10 10 , 11 11 , 13 13 , 16 16 , . . . ...

which one may recognize as the beautiful Thue-Morse sequence!! where a 1 1 appears at index n n if the first player wins when given n n coins, and a 0 0 appears otherwise.

[The Thue-Morse sequence is computed by starting with an initial 0 0 , and repeatedly appends the negation of the already-found sequence, i.e., [ 0 ] [ 1 ] [ 10 ] [ 1001 ] [ 10010110 ] [ 1001011001101001 ] [0][1][10][1001][10010110][1001011001101001] , etc.]

The Thue-Morse sequence has many interesting properties, which makes this connection so amazing; examples are that any substring is not repeated more than twice in a row and that it can be neatly applied to the fair-sharing problem and the equal-sums-of-powers problem. But the property that allows us to finish the problem is that there is an explicit formula for each digit of the sequence: the digit at index n n is equal to the number, modulo 2 2 , of ones in the binary representation of n 1 n-1 .

Since 420 1 = 419 = 110100011 2 420-1=419={110100011}_2 has 5 5 ones in its binary representation, the 420 420 th digit of the Thue-Morse sequence is a 1 1 , and therefore the first player (Sharky) wins when starting with 420 420 coins.

Anton Wu - 4 years ago

Great question with an even greater solution!!!

Vaibhav Thakkar - 4 years ago

This question "who has the winning strategy?" makes no sense! You only declared rules of the game, and who goes first... it would be really helpful to re-phrase this problem.

Sarah Mausolf - 4 years ago

Log in to reply

"Who has the winning strategy?" means to ask who will be the winner if they chose to play optimally. This is standard in most questions of this type.

Sharky Kesa - 4 years ago

Log in to reply

The language is typical, but in this case Sharky doesn't have a losing strategy. There are exactly five legal moves of two types. The game will end after five moves regardless of what Sharky decides to do.

Richard Desper - 4 years ago

Oh... i misunderstood the prob. In operation 1, if 2^n < k, with n being largest possible... i thought that n has to be removed.. not 2^n . Thanks..

Ananya Aaniya - 4 years ago

This shouldn't be a Level-1 question..!!

Anees Parwez - 4 years ago

Log in to reply

It was a level 4, but as you see, it became the "Problem of the Week" so people start solving them (about 4000 solvers and counting) That's simply why this is a level 1.

The names should be changed by the way. I think a lot guessed the answer as Sharky ;)

Steven Jim - 4 years ago

Great solution SHARKY!!!! But why did you made the character lose who has been named after you!!! Ha ha ha

Prayas Rautray - 3 years, 11 months ago

Log in to reply

Sorry sorry it's win win

Prayas Rautray - 3 years, 11 months ago

The trick is to immediately convert 420 into binary 110100100. The two rules then become

  1. Remove the left-most 1 (and any 0s between that and the next 1)

  2. Remove the right-most 0

Whichever order the players choose to do these steps, it will take exactly five steps to reduce the number to a single 1.

Paul Cockburn - 2 years, 8 months ago
Leonard Ng
May 29, 2017

Please let me know if there are mistakes in my reasoning:

For me, I see 420 = 2^8 + 2^7 + 2^5 + 2^2

Operation 1 is equivalent to removing the 1st term (the left most one). Operation 2 is equivalent to reducing the powers of every term by one.

The game comes to an end when a player is left with exactly one coin, which is the only situation where no legal moves are possible.

Operations 1 or 2 may be carried out in any order, but operation 2 will be executed exactly *twice * (need not be consecutively), after which the last term ends up becoming 1, making the sum to be odd and therefore making it impossible to perform operation 2 according to its rule. As for operation 1, it will be executed exactly 3 times , after which there will only be 1 term left.

Hence, after exactly 5 moves (3 times of op. 1 and 2 times of op. 2 regardless of order), only 1 coin will be left. The player who has to make the 6th move will not have a legal move and will therefore lose the game. Since 6 is an even number, the one who makes the first move will win. Congratulations, Sharky!

A perfectly right and beautifull solution! As far as I understand, the logic of your solution is the same as that of Sharky Kesa's solution, presented in a different and interesting way

Log in to reply

Thank you! Yes, the underlying logic is the same, just that I did not use binary expressions.

Leonard Ng - 4 years ago

This is really lovely solution! Thanks for sharing

Jacob Strauss - 4 years ago

Log in to reply

You are welcome!

Leonard Ng - 4 years ago

Awesome...Awesome...Awesome... don't doubt your reasoning. This is the best explanation to this question.

Saransh Dave - 4 years ago

Log in to reply

Thank you!

Leonard Ng - 4 years ago
Bob Liddington
May 29, 2017

Not such an elegant, general solution, but all roads seem to lead rapidly to a Sharky win!!

Step 0....S .....................420

Step 1....I.............164 .......or........ 210

Step 3...S......... 26...or..........82....or....105

Step 3...I .........13...or...10,......18...or....41

Step 4...S.............5......or....2.......or .... 9

Step 5....I ...........................1!!!!

It is a good solution

Mritunjay Mehta - 4 years ago

Log in to reply

The 26 messes things up. There's no path to getting to 26, 13, 10, or 5 if you start with 420 coins.

Richard Desper - 4 years ago

Its a good solution..but a small correction: 164-128 = 36 not 26

I worked on the solution the same way that you did but I got Ivan has more winning paths. Am I missing something here?

A Former Brilliant Member - 3 years, 12 months ago

One huge flaw in the solution. Either one of them could win. It says one can take half the coins if there is an even number. Sharky can take 210; Ivan can take the other 210 and win.

Sara Flint - 4 years ago

Log in to reply

Wrong. When it's Ivan's turn, he is facing 210 coins, so he can either take 105 or 128 coins, not the whole thing.

Kenny Wong - 4 years ago
Arjen Vreugdenhil
May 30, 2017

There is no winning "strategy" in a sense-- no matter what choices the player makes, Ivan will lose at his third turn.

Let N N be the number of coins in the heap, and resolve it in the form N = k S 2 k , N = \sum_{k \in S} 2^k, where S S is a set of non-zero integers. In this case, N = 420 = 2 8 + 2 7 + 2 5 + 2 2 N = 420 = 2^8 + 2^7 + 2^5 + 2^2 so that S = { 8 , 7 , 5 , 2 } S = \{8,7,5,2\} .

Let's consider the effect of each valid moves on the set S S : they are

  • remove the largest value from S S , provided S > 1 |S| > 1 ;

  • subtract one from each element of S S , provided 0 ∉ S 0 \not\in S .

The first operation will decrease S |S| (the number of elements in S S ) by one but not affect min S \min S ; the second operation will decrease min S \min S by one, but not affect S |S| . Therefore the characteristic value c = S + min S c = |S| + \min S will decrease by one in every turn. Moreover, a player can only lose if both S = 1 |S| = 1 and 0 S 0 \in S , i.e. when c = 1 c = 1 . Therefore, given an initial situation with characteristic c c , we know that there are c 1 c - 1 moves before the game ends.

In this situation, c = 4 + 2 c = 4 + 2 , so that there are 5 5 possible moves before Ivan \boxed{\text{Ivan}} loses.

Peter Macgregor
Jun 1, 2017

This is no match for Sharky's lovely solution of the general case, but more modestly solves the game from the specified starting point.

We can start with three observations and then show that Sharky \boxed{\text{ Sharky }} has a winning strategy.

First note that if there is one coin in the pile there is no legal move.

Next note that if the pile has more than one coin there will be a legal move.

These two observations together show that the loser will be the first person confronted with a pile containing one coin.

Finally note that once there is an odd number of coins in the pile, the number of coins remains odd for the rest of the game. This is because move-option 2 is not permissible and move-option 1 involves subtracting an even power of 2. An odd number minus an even number always produces another odd number. So once there is an odd number of coins in the pile, the moves of both players are forced to be move-option 1.

Now here is Sharky's strategy. He should half the number of coins leaving 210 coins. Now if Ivan halves the number to leave 105 in the pile we are onto an odd number chain and there are no more choices to be made. The sequence goes

420-210-105-41-9-1

If instead of halving the 210, Ivan takes away 128 coins, Sharky should halve the result which switches the game onto the odd number track. The sequence now goes

420-210-82-41-9-1

Both of these sequences have five moves, so the next move is Ivan's. But since he is confronted with a pile of one coin he has no legitimate move and so looses the game.

Richard Desper
Jun 3, 2017

Not that we really need another solution, but the 3 by 4 grid below shows all the possible states:

420 & 164 & 36 & 4

210 & 82 & 18 & 2

105 & 41 & 9 & 1

At any state there are two possible moves: to the right or down. Subtracting the largest power of two is a move to the right. Dividing by two is a move down. These moves commute with each other, i.e. subtracting the largest power of two followed by a division by two leads one to the same point as doing the same operations in the opposite order. Of course if I listed all the numbers by their binary representation, the nature of the grid would be more obvious.

110100100 & 10100100 & 100100 & 100

11010010 & 1010010 & 10010 & 10

1101001 & 101001 & 1001 & 1

The move to the right is accomplished by removing the leading '1' while the move down is accomplished by removing the last '0'.

Gopal Muthyala
Jun 2, 2017

I got it right with what i could understand, but want to make sure i understood correct,

Given k = 420, largest power of 2 < k = 2^8 = 256

  1. Given Sharky is going to start first, he can remove 256 coins. which leaves us with 420 - 256 = 164
  2. Ivan is left with 164 and can remove max of 128 which is 2^7 which leaves remaining coin count to 164 - 128 = 36
  3. Sharky now left with 36 coins can remove 2^5 = 32, with 4 coins remain
  4. Ivan now left with 4 coins can make a legal move by removing 2^2 = 4 leaving 0 coins.
  5. Sharky cannot make any legal move now,

Am left with 2 questions, what is a winning strategy here? And why Sharky is on Winning Strategy?

Firstly, Ivan can't remove 4 coins, because it isn't the largest power of 2 less than k k (instead, it is equal to k k ). Secondly, winning strategy means, who is the winner given both play optimally.

Sharky Kesa - 4 years ago
Samuel Qin
May 30, 2017

For a person to lose, they must either end with a odd number

We immediately notice that removing a power of 2 does not "remove a two", that is, cause the number to lose a power of two. We also notice

420=2^2 3 5*7

thus, Sharky will always have the upper hand by forcing Ivan to have a odd number.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...