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

Constraints

Example

Input
n = 8
Output
false

Solve it

More in Game Theory

All algorithm problems