Search⌘ K
AI Features

Solution: Nim Game

Understand the mathematical insight behind the Nim Game, where the player can win by checking if the number of stones is not divisible by four. This lesson teaches you to apply game theory and optimal strategies to solve the problem in constant time.

Statement

You and a friend are playing a game called the Nim Game with a heap of stones. The rules are as follows:

  • There is a single heap containing n stones.

  • You and your friend take alternating turns, with you going first.

  • On each turn, the current player must remove between ...