All Euler problems
Project Euler

Repunit Divisibility

A repunit R_n is a number consisting of n ones: R_n = underbrace11ldots1_n = (10^n - 1)/(9). Find the sum of all n <= 1000 such that R_n is divisible by n.

Source sync Apr 19, 2026
Problem #0961
Level Level 17
Solved By 802
Languages C++, Python
Answer 166666666689036288
Length 256 words
modular_arithmeticnumber_theorybrute_force

Problem Statement

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

This game starts with a positive integer. Two players take turns to remove a single digit from that integer. After the digit is removed any resulting leading zeros are removed.

For example, removing a digit from \(105\) results in either \(5\), \(10\) or \(15\).

The winner is the person who removes the last nonzero digit.

Define \(W(N)\) to be how many positive integers less than \(N\) for which the first player can guarantee a win given optimal play. You are given \(W(100) = 18\) and \(W(10^4) = 1656\).

Find \(W(10^{18})\).

Problem 961: Repunit Divisibility

Mathematical Foundation

Theorem 1 (Divisibility Criterion). Let gcd(n,10)=1\gcd(n, 10) = 1. Then nRnn \mid R_n if and only if 10n1(mod9n)10^n \equiv 1 \pmod{9n}.

Proof. We have Rn=(10n1)/9R_n = (10^n - 1)/9. Since gcd(n,10)=1\gcd(n, 10) = 1 implies gcd(n,9)9\gcd(n, 9) \mid 9, we compute:

nRn    n10n19    9n(10n1)    10n1(mod9n).n \mid R_n \iff n \mid \frac{10^n - 1}{9} \iff 9n \mid (10^n - 1) \iff 10^n \equiv 1 \pmod{9n}.

The second equivalence holds because 9(10n1)9 \mid (10^n - 1) is always true (since 101(mod9)10 \equiv 1 \pmod{9}, so 10n1(mod9)10^n \equiv 1 \pmod{9}), hence n(10n1)/9n \mid (10^n - 1)/9 if and only if 9n(10n1)9n \mid (10^n - 1). \square

Lemma 1 (Even and 5-divisible exclusion). If n>1n > 1 and 2n2 \mid n or 5n5 \mid n, then nRnn \nmid R_n.

Proof. Every repunit consists entirely of the digit 1, so RnR_n is odd for all n1n \ge 1 (since Rn=111nR_n = \underbrace{11\ldots1}_{n} and the last digit is 1). Similarly, Rn1(mod5)R_n \equiv 1 \pmod{5} for all n1n \ge 1. Therefore 2Rn2 \nmid R_n and 5Rn5 \nmid R_n, which means if 2n2 \mid n or 5n5 \mid n (with n>1n > 1), then nRnn \nmid R_n. \square

Theorem 2 (Powers of 3). For all k1k \ge 1, 3kR3k3^k \mid R_{3^k}.

Proof. By induction on kk.

Base case (k=1k = 1): R3=111=3×37R_3 = 111 = 3 \times 37, so 3R33 \mid R_3.

Inductive step: Assume 3k1R3k13^{k-1} \mid R_{3^{k-1}}. Using the factorization identity for repunits:

R3m=Rm(102m+10m+1),R_{3m} = R_m \cdot (10^{2m} + 10^m + 1),

set m=3k1m = 3^{k-1}, so R3k=R3k1(1023k1+103k1+1)R_{3^k} = R_{3^{k-1}} \cdot (10^{2 \cdot 3^{k-1}} + 10^{3^{k-1}} + 1).

Since 101(mod3)10 \equiv 1 \pmod{3}, we have 1023k1+103k1+11+1+1=30(mod3)10^{2 \cdot 3^{k-1}} + 10^{3^{k-1}} + 1 \equiv 1 + 1 + 1 = 3 \equiv 0 \pmod{3}.

By the inductive hypothesis 3k1R3k13^{k-1} \mid R_{3^{k-1}}, and the cofactor is divisible by 3, so 3kR3k3^k \mid R_{3^k}. \square

Lemma 2 (Multiplicative order characterization). For gcd(n,10)=1\gcd(n, 10) = 1, let d=ord9n(10)d = \operatorname{ord}_{9n}(10) be the multiplicative order of 10 modulo 9n9n. Then nRnn \mid R_n if and only if dnd \mid n.

Proof. By Theorem 1, nRn    10n1(mod9n)n \mid R_n \iff 10^n \equiv 1 \pmod{9n}. By definition of multiplicative order, 10n1(mod9n)10^n \equiv 1 \pmod{9n} if and only if dnd \mid n. \square

Editorial

R_n = (10^n - 1) / 9 = 111…1 (n ones). Find sum of all n <= 1000 such that n | R_n. Equivalently: 9n | (10^n - 1), i.e., 10^n = 1 (mod 9n). Only possible when gcd(n, 10) = 1 (n odd, not divisible by 5). Complexity: O(N log N) with modular exponentiation.

Pseudocode

    total = 0
    For n from 1 to N:
        If gcd(n, 10) != 1 then
            continue
        If ModPow(10, n, 9*n) == 1 then
            total = total + n
    Return total

    result = 1
    base = base mod mod
    While exp > 0:
        if exp is odd:
            result = (result * base) mod mod
        exp = exp >> 1
        base = (base * base) mod mod
    Return result

Complexity Analysis

  • Time: O(NlogN)O(N \log N). For each of the O(N)O(N) candidate values of nn, binary modular exponentiation computes 10nmod9n10^n \bmod 9n in O(logn)O(\log n) multiplications.
  • Space: O(1)O(1). Only a constant number of variables are maintained.

Answer

166666666689036288\boxed{166666666689036288}

Code

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

C++ project_euler/problem_961/solution.cpp
/*
 * Problem 961: Repunit Divisibility
 *
 * Find sum of n <= 1000 such that n | R_n where R_n = (10^n - 1)/9.
 * Equivalently: 10^n = 1 (mod 9n), for gcd(n, 10) = 1.
 *
 * Complexity: O(N log N) with binary exponentiation.
 */

#include <bits/stdc++.h>
using namespace std;

long long power(long long base, long long exp, long long mod) {
    long long result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = result * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return result;
}

int main() {
    const int N = 1000;
    long long total = 0;

    for (int n = 1; n <= N; n++) {
        if (n % 2 == 0 || n % 5 == 0) continue;  // gcd(n,10) > 1
        long long mod = 9LL * n;
        if (power(10, n, mod) == 1) {
            total += n;
        }
    }

    // n=1 is special: R_1 = 1, 1|1
    // Already handled since gcd(1,10)=1 and 10^1 mod 9 = 1

    cout << total << endl;
    return 0;
}