Problem 61008. Identify winnable games of Take a Square
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
Christian Schröder
on 21 Sep 2025 at 16:50
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.
ChrisR
on 21 Sep 2025 at 22:06
Thanks Christian. I changed the description.
Solution Comments
Show commentsProblem Recent Solvers3
Suggested Problems
-
Identify winnable games of Take a Square
3 Solvers
More from this Author311
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!