Your friend decided to play a game with you. He asked you to pick an integer between 1 to 50 inclusive, let's say you pick the integer .
Now you are played against your friend such that both of you take turns subtracting a non-zero perfect square not larger than , the first person who cannot make the subtraction loses.
You're the first person to subtract first, then your friend, then you, and so on.
How many possible number are there for you to pick (from the start) such that you will always win the game?
Assume both of you intends to win the game and plays optimally.
Details and Assumptions :
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.
If both players play optimally: A winning number is a number that if chosen, will guarantee a win. A losing number is a number if chosen, will guarantee a loss.
I solved this manually using a spreadsheet. I was then inspired to write the algorithm as a Python code to calculate the winning numbers for the first n positive integers.
To check whether a number a is winning or losing, all perfect squares less than a are subtracted from a one at a time. This gives all the possible outcomes for the second player. If all possible outcomes are winning numbers, then the second player has won. Otherwise, the first player wins.
Output :
[1, 3, 4, 6, 8, 9, 11, 13, 14, 16, 18, 19, 21, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 40, 41, 42, 43, 45, 46, 47, 48, 49, 50]
38
Answer: 3 8