All Euler problems
Project Euler

Nim

Nim is a two-player combinatorial game played with heaps of stones under the normal play convention: players alternate turns removing any positive number of stones from a single heap, and the playe...

Source sync Apr 19, 2026
Problem #0301
Level Level 05
Solved By 7,355
Languages C++, Python
Answer 2178309
Length 464 words
game_theorysequencemodular_arithmetic

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Nim is a game played with heaps of stones, where two players take it in turn to remove any number of stones from any heap until no stones remain.

We’ll consider the three-heap normal-play version of Nim, which works as follows:

  • At the start of the game there are three heaps of stones.

  • On each player’s turn, the player may remove any positive number of stones from any single heap.

  • The first player unable to move (because no stones remain) loses.

If \((n_1, n_2, n_3)\) indicates a Nim position consisting of heaps of size \(n_1\), \(n_2\) and \(n_3\) then there is a simple function, which you may look up or attempt to deduce for yourself, \(X(n_1, n_2, n_3)\) that returns:

  • zero if, with perfect strategy, the player about to move will eventually lose; or

  • non-zero if, with perfect strategy, the player about to move will eventually win.

For example \(X(1, 2, 3) = 0\) because, no matter what the current player does, the opponent can respond with a move that leaves two heaps of equal size, at which point every move by the current player can be mirrored by the opponent until no stones remain; so the current player loses. To illustrate:

  • current player moves to \((1, 2, 1)\)

  • opponent moves to \((1, 0, 1)\)

  • current player moves to \((0, 0, 1)\)

  • opponent moves to \((0, 0, 0)\), and so wins.

For how many positive integer \(n \leq 2^{30}\) does \(X(n, 2n, 3n) = 0\)?

Problem 301: Nim

Mathematical Development

Definition. For a non-negative integer nn, let β(n)=(bk,bk1,,b0)\beta(n) = (b_k, b_{k-1}, \ldots, b_0) denote its binary representation, where bi{0,1}b_i \in \{0,1\} and n=i=0kbi2in = \sum_{i=0}^{k} b_i \cdot 2^i.

Lemma 1 (Carry-free addition). For non-negative integers aa and bb, the identity ab=a+ba \oplus b = a + b holds if and only if a&b=0a \mathbin{\&} b = 0, where &\mathbin{\&} denotes bitwise AND.

Proof. Write a=iai2ia = \sum_{i} a_i 2^i and b=ibi2ib = \sum_{i} b_i 2^i with ai,bi{0,1}a_i, b_i \in \{0,1\}. Bitwise XOR computes columnwise addition modulo 2 without propagating carries: (ab)i=ai+bi(mod2)(a \oplus b)_i = a_i + b_i \pmod{2}. The ordinary sum satisfies a+b=i(ai+bi)2ia + b = \sum_i (a_i + b_i) 2^i only when no carry is generated, which occurs precisely when ai+bi1a_i + b_i \le 1 for all ii, i.e., aibi=0a_i \cdot b_i = 0 for all ii. This is equivalent to a&b=0a \mathbin{\&} b = 0. \square

Theorem 1 (Characterization of losing positions). n2n3n=0n \oplus 2n \oplus 3n = 0 if and only if nn has no two consecutive 1-bits in its binary representation.

Proof. Since 3n=n+2n3n = n + 2n, the condition n2n3n=0n \oplus 2n \oplus 3n = 0 is equivalent to n2n=3n=n+2nn \oplus 2n = 3n = n + 2n. By Lemma 1, this holds if and only if n&2n=0n \mathbin{\&} 2n = 0.

Now observe that multiplication by 2 corresponds to a left shift by one bit: bit ii of 2n2n equals bit (i1)(i-1) of nn. Therefore

n&2n=i1bibi12i,n \mathbin{\&} 2n = \sum_{i \ge 1} b_i \cdot b_{i-1} \cdot 2^i,

where n=ibi2in = \sum_i b_i \cdot 2^i. This vanishes if and only if bibi1=0b_i \cdot b_{i-1} = 0 for all i1i \ge 1, which is precisely the condition that no two consecutive bits of nn are both 1. \square

Lemma 2 (Fibonacci counting). Let aka_k denote the number of binary strings of length kk (including leading zeros) with no two consecutive 1-bits. Then ak=Fk+2a_k = F_{k+2}, where (Fn)n1(F_n)_{n \ge 1} is the Fibonacci sequence defined by F1=F2=1F_1 = F_2 = 1 and Fn=Fn1+Fn2F_n = F_{n-1} + F_{n-2} for n3n \ge 3.

Proof. We proceed by strong induction. A valid kk-bit string either:

  • begins with 00: the remaining k1k-1 bits form any valid (k1)(k-1)-bit string, yielding ak1a_{k-1} choices; or
  • begins with 11: the second bit must be 00 (to avoid consecutive 1s), and the remaining k2k-2 bits form any valid (k2)(k-2)-bit string, yielding ak2a_{k-2} choices.

Hence ak=ak1+ak2a_k = a_{k-1} + a_{k-2} for k3k \ge 3. The base cases are a1=2a_1 = 2 (strings {0,1}\{0, 1\}) and a2=3a_2 = 3 (strings {00,01,10}\{00, 01, 10\}). Since F3=2=a1F_3 = 2 = a_1 and F4=3=a2F_4 = 3 = a_2, the recurrence and initial conditions match, giving ak=Fk+2a_k = F_{k+2} by induction. \square

Corollary. The number of non-negative integers in {0,1,,2k1}\{0, 1, \ldots, 2^k - 1\} with no two consecutive 1-bits is Fk+2F_{k+2}.

Proof. The integers in {0,1,,2k1}\{0, 1, \ldots, 2^k - 1\} correspond bijectively to kk-bit strings (with leading zeros). Apply Lemma 2. \square

Theorem 2 (Main result). The number of positive integers n230n \le 2^{30} satisfying n2n3n=0n \oplus 2n \oplus 3n = 0 is F32=2178309F_{32} = 2178309.

Proof. By the Corollary, the qualifying integers in {0,1,,2301}\{0, 1, \ldots, 2^{30} - 1\} number F32F_{32}. Excluding n=0n = 0 yields F321F_{32} - 1 qualifying positive integers below 2302^{30}. The integer n=230n = 2^{30} has binary representation 1000301\underbrace{00\cdots0}_{30}, which trivially has no two consecutive 1-bits. Including it yields F321+1=F32=2178309F_{32} - 1 + 1 = F_{32} = 2178309. \square

Editorial

The position (n, 2n, 3n) is a P-position iff n XOR 2n XOR 3n = 0, which holds iff n has no two consecutive 1-bits in binary. The count of such n in [1, 2^30] equals the Fibonacci number F(32).

Pseudocode

    Set a <- 1; b <- 1 // F(1) = F(2) = 1
    For i from 3 to 32:
        (a, b) <- (b, a + b) // advance Fibonacci pair
    Return b // F(32)

Complexity Analysis

  • Time: O(1)O(1) — computing F32F_{32} requires exactly 30 additions of bounded integers.
  • Space: O(1)O(1) — two integer variables suffice.

Answer

2178309\boxed{2178309}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_301/solution.cpp
#include <cstdio>

/*
 * Problem 301: Nim
 *
 * (n, 2n, 3n) is a P-position iff n XOR 2n XOR 3n = 0,
 * equivalently n has no two consecutive 1-bits in binary.
 * Count of such n in [1, 2^30] = F(32) = 2178309.
 */

int main() {
    long long a = 1, b = 1;
    for (int i = 3; i <= 32; i++) {
        long long c = a + b;
        a = b;
        b = c;
    }
    printf("%lld\n", b);
    return 0;
}