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

100.0% Correct | 0.0% Incorrect
Last Solution submitted on Sep 23, 2025

Problem Comments

Solution Comments

Show comments

Problem Recent Solvers3

Suggested Problems

More from this Author311

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!