Nim Game — Game Theory
Given a stone pile, decide whether the first mover can guarantee taking the final stone when both sides play perfectly.
Game Theory · Easy
The problem
A friend offers you a stone-taking bet and lets you move first; you want to know for which heap sizes you can force a win. You and a friend take turns removing stones from a single heap, and you go first. On each turn the mover must take 1, 2 or 3 stones, and whoever takes the last stone wins. Both players play perfectly.
The story
A friend offers you a bet on a pile of n stones, with n from 1 to 1000000000, and lets you go first. You take turns removing 1, 2 or 3 stones, and whoever takes the last stone wins. If both of you play perfectly, can you force a win?
What to return
- Return true if the first player can force a win from a heap of n stones, otherwise false.
Constraints
- 1 <= n <= 1000000000
- Your code may run at most 4 counted steps on the largest case.
Example
- Input
n = 8- Output
false