Math game problem, not able to understand why the solution works?

54 Views Asked by At

Players A and B

Rules of the game:

  • The game is played with two piles of matches. Initially, the first pile contains N matches and the second one contains M matches.

  • The players alternate turns; A plays first.

  • On each turn, the current player must choose one pile and remove a
    positive number of matches (not exceeding the current number of
    matches on that pile) from it.

  • It is only allowed to remove X matches from a pile if the number of
    matches in the other pile divides X.

  • The player that takes the last match from any pile wins.

The solution i have thought for this problem is:

if n % m == 0 or m % n== 0 then player A will win
else we will find the reminder and quotient and count the moves

if quotient is > 1: and move is Odd then player A wins

                 2: move is even then player B wins

else swap the larger pile and smaller pile and repeater the above steps