1
$\begingroup$

Jacob and Vicky play the fun game of multiplication by multiplying an integer p by one of the numbers 2 to 9. Jacob always starts with p = 1, does his multiplication, then Vicky multiplies the number, then Jacob and so on. The winner is who first reaches p >= n (when n is a number chosen at the beginning of each game n>1). Assuming that both of them play perfectly (i.e. Jacob and Vicky play to win, and follow the perfect strategy to win ). Write a Python function play_fun_game, that consume a positive integer n and produces True if Vicky wins after playing the game according to the explained rules, otherwise the function produces False.

These are the examples given with the question.

play_fun_game(17) => True

play_fun_game(35) => False

play_fun_game(190) => True

play_fun_game(771) => False

play_fun_game(20) => False

play_fun_game(3480) => True

play_fun_game(1589) => False

play_fun_game(5768) => True

play_fun_game(36) => False

play_fun_game(2222) => False

play_fun_game(3489) => True

I've determined the following ranges for winners: [1, 9] => Jacob [10, 18] => Vicky [19, 162] => Jacob [163, 324] => Vicky [325, 2916] => Jacob etc.

I noticed that the pattern for the ranges above is that 9x2 to get 18 and 18x9 to get 162. Again, 162x2 to get 324 and 324*9 to get 2916. I have to write a recursive function in Python for this game and I have no clue where to start.

$\endgroup$

1 Answer 1

0
$\begingroup$

It's quite easy given the pattern you have found:

def play_fun_game(n):
    if(1<=n<=18): # Base case:
        ans = (n>9)
    else: # Recursive case:
        ans = play_fun_game(n//18)
    return ans

In short version:

def play_fun_game(n):
    return (n>9) if(1<=n<=18) else play_fun_game(n//18)

It works because each full cycle happens after multiplying by 2 and then by 9, which is 18.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .