Walking on thin ice

Logic Level 5

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 N N .

Now you are played against your friend such that both of you take turns subtracting a non-zero perfect square not larger than N N , 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 :

  • As an explicit example, suppose you pick the number n = 6 n=6 . Then you can subtract 4, the resultant number becomes 2. Your friend then subtract it by 1, leaving the number to be 1. And lastly, you are allowed to subtract it by 1 one more time. So your opponent can't make the subtraction anymore because the final number is 0. Thus he loses. This means that I can always win the game if I pick the integer 6 6 .


The answer is 38.

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.

2 solutions

Pranshu Gaba
Jul 12, 2015

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 n positive integers.

To check whether a number a a is winning or losing, all perfect squares less than a a are subtracted from a 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# append function is defined that appends b
# to list a only if it is not already present in a
def append(a, b):
    if b not in a:
        a.append(b)
    return a

def game(n):
    wl = [] # winning list
    ll = [] # losing list
    sl = [] # square list

# adds all perfect square numbers less than n to sl
    x = 1
    while x <= n:
        if x**(0.5) % 1 == 0:
            append(sl, x)
        x += 1

    a = 1
    while a <= n:
        if a in sl:
            append(wl, a)
        else:
            ssl = []  # subtracted square list
            for i in sl:
                append(ssl, a - i)

# ssl with only positive terms retained
            pssl = [item for item in ssl if item >= 0] 

# counts  how many losing numbers in pssl
            m = 0
            for c in pssl:
                if c in ll:
                    m = m + 1

# if there are no losing numbers, then a is a losing number.                    
            if m == 0:
                append(ll, a)
# otherwise a is a winning number
            else:
                append(wl, a)

        a += 1
    return wl

print(game(50))
print(len(game(50)))

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: 38 \boxed{38}

Moderator note:

Solving by hand is always possible but takes more effort. For all of these numbers found, is there a specific order of perfect squares I need to subtract first?

I feel doing it manually is easier by the following method : 1. List the results from numbers 1 to 13 with their wins(W) and losses(L)

2. Now observe that every number with a loss(L) is always followed by a number with win.

3. Starting with any particular n (13 onward ) subtract consecutive odd numbers 1,3,5... and check whether resulting intermediate number is a losing number(L) ... If yes then n is a winning number .... If we can't find any losing number (L) then n is a losing number.

With this algorithm and a bit of concentration we can tackle the problem efficiently

Abhinav Raichur - 5 years, 10 months ago

Log in to reply

Did it manually and it took less effort.

Vinayak Verma - 5 years, 7 months ago

WOAH! THANKYOU

Pi Han Goh - 5 years, 11 months ago

8 cant be a number..! since if i take 8 and subtract 4.! the resultant is 4.! and the opponent can take value smaller than or equal to the resultant(not greater than) therefore, i take 8 subtract 4 then opponent subtract 4.! game over i lose.!

Tirth Patel - 5 years, 9 months ago

Log in to reply

We make use of this statement given in the problem:

Assume both of you intends to win the game and plays optimally.

The strategy that you outlined is a losing one and I wouldn't adopt it because there exists a winning strategy. I take 8, subtract 1. You have 7. Now there is no way you can win. If you subtract 4, I will subtract 1. If you subtract 1, I will subtract 4. Either way, you are left with 2, you have to subtract 1. I get 1, and subtract 1 and win.

Pranshu Gaba - 5 years, 9 months ago
Sanchit Aggarwal
Jul 15, 2015

I did this, manually, i.e number by number. Took less than 18 minutes.

I did this manually at 12:30 and it was like half an hour for me because my first answer 37 was wrong. And i did thw whole thing AGAIN!

Arpit MIshra - 5 years, 8 months ago

Not recommended. I did this with a computer, it took 7 minutes

Agnishom Chattopadhyay - 5 years, 7 months ago

Did it manually in 8 minutes

Meet Patel - 2 years, 4 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...