A game has a pile of tokens, and two players alternate in taking a certain number of tokens from the pile, with the constraint that the number has to be a non-zero perfect square. A player loses if no tokens remain to be taken.
For example, if the game starts with 4 tokens in the pile, then Player 1 can win by taking all of them. If the game starts with 14 tokens, Player 1 can win by taking 9, leaving 5. Player 2 must then take either 1 or 4. Either way, Player 1 wins by taking all of the remaining tokens.
Write a function that takes the initial number of tokens in the pile and returns true if Player 1 can force a win by playing optimally.
Solution Stats
Problem Comments
2 Comments
Solution Comments
Show comments
Loading...
Problem Recent Solvers4
Suggested Problems
-
Identify winnable games of Take a Square
4 Solvers
More from this Author321
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Great problem, Chris, I really enjoyed this. Nitpick: the function should return true if Player 1 can *force* a win, not if they can win.
Thanks Christian. I changed the description.