All Euler problems
Project Euler

Repunit Divisibility

A number consisting entirely of ones is called a repunit. Define R(k) = underbrace11ldots1_k. Given that n is a positive integer and gcd(n, 10) = 1, there always exists k such that n | R(k). Let A(...

Source sync May 21, 2026
Problem #0129
Level Level 05
Solved By 7,283
Languages C++, Python
Answer 1000023
Length 337 words
modular_arithmeticnumber_theorysequence

Problem Statement

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

A number consisting entirely of ones is called a repunit. We shall define \(R(k)\) to be a repunit of length \(k\); for example, \(R(6) = 111111\).

Given that \(n\) is a positive integer and \(\gcd (n, 10) = 1\), it can be shown that there always exists a value, \(k\), for which \(R(k)\) is divisible by \(n\), and let \(A(n)\) be the least such value of \(k\); for example, \(A(7) = 6\) and \(A(41) = 5\).

The least value of \(n\) for which \(A(n)\) first exceeds ten is \(17\).

Find the least value of \(n\) for which \(A(n)\) first exceeds one-million.

Problem 129: Repunit Divisibility

Mathematical Development

Definition. The repunit of length kk is R(k)=10k−19R(k) = \frac{10^k - 1}{9}.

Theorem 1 (Existence of A(n)A(n)). For any positive integer nn with gcd⁡(n,10)=1\gcd(n, 10) = 1, A(n)A(n) exists and A(n)≤nA(n) \leq n.

Proof. Consider the sequence rk=R(k) mod nr_k = R(k) \bmod n for k=1,2,…,n+1k = 1, 2, \ldots, n+1. This sequence takes values in {0,1,…,n−1}\{0, 1, \ldots, n-1\}.

Case 1: If rk=0r_k = 0 for some k≤nk \leq n, then n∣R(k)n \mid R(k) and A(n)≤nA(n) \leq n.

Case 2: If rk≠0r_k \neq 0 for all k≤nk \leq n, then by the pigeonhole principle, there exist 1≤i<j≤n+11 \leq i < j \leq n+1 with ri=rjr_i = r_j. Now:

R(j)−R(i)=11…1⏟j−11…1⏟i=11…1⏟j−i⋅10i=R(j−i)⋅10i.R(j) - R(i) = \underbrace{11\ldots1}_{j} - \underbrace{11\ldots1}_{i} = \underbrace{11\ldots1}_{j-i} \cdot 10^i = R(j-i) \cdot 10^i.

Since n∣(R(j)−R(i))n \mid (R(j) - R(i)) and gcd⁡(10i,n)=1\gcd(10^i, n) = 1 (because gcd⁡(10,n)=1\gcd(10, n) = 1), we get n∣R(j−i)n \mid R(j-i), where 1≤j−i≤n1 \leq j - i \leq n. Thus A(n)≤nA(n) \leq n. □\square

Theorem 2 (Connection to multiplicative order). Let ordm(10)\text{ord}_m(10) denote the multiplicative order of 10 modulo mm. Then:

  • If gcd⁡(n,9)=1\gcd(n, 9) = 1: A(n)=ordn(10)A(n) = \text{ord}_n(10).
  • If gcd⁡(n,9)=3\gcd(n, 9) = 3: A(n)=ordn(10)A(n) = \text{ord}_n(10).
  • If 9∣n9 \mid n: A(n)=ordn(10)A(n) = \text{ord}_{n}(10) (when defined appropriately via the recurrence).

In general, A(n)∣lcm(ordn/gcd⁡(n,9)(10),ordgcd⁡(n,9)(10))A(n) \mid \text{lcm}(\text{ord}_{n/\gcd(n,9)}(10), \text{ord}_{\gcd(n,9)}(10)), but it is simplest to compute A(n)A(n) directly.

Proof. R(k)=(10k−1)/9R(k) = (10^k - 1)/9. If gcd⁡(n,9)=1\gcd(n, 9) = 1, then n∣R(k)⇔n∣(10k−1)⇔10k≡1(modn)n \mid R(k) \Leftrightarrow n \mid (10^k - 1) \Leftrightarrow 10^k \equiv 1 \pmod{n}, so A(n)=ordn(10)A(n) = \text{ord}_n(10). The cases where 3∣n3 \mid n require analyzing the interaction of factors of 3 in nn and in 99, but the iterative computation handles all cases uniformly. □\square

Lemma 1 (Iterative computation). A(n)A(n) can be computed via the recurrence r1=1 mod nr_1 = 1 \bmod n, rk=(10rk−1+1) mod nr_k = (10 r_{k-1} + 1) \bmod n, and A(n)A(n) is the smallest kk with rk=0r_k = 0.

Proof. We have R(1)=1R(1) = 1 and R(k)=10⋅R(k−1)+1R(k) = 10 \cdot R(k-1) + 1. Working modulo nn preserves the recurrence: rk≡R(k)(modn)r_k \equiv R(k) \pmod{n}. The first kk with rk=0r_k = 0 is exactly A(n)A(n). □\square

Theorem 3 (Search bound). Since A(n)≤nA(n) \leq n (Theorem 1) and we need A(n)>106A(n) > 10^6, any solution must satisfy n>106n > 10^6.

Proof. Immediate from A(n)≤nA(n) \leq n. □\square

Editorial

Because Theorem 1 guarantees A(n)≤nA(n) \le n, any value with A(n)>106A(n) > 10^6 must itself lie above 10610^6. That means the search can begin immediately at 106+110^6+1. There is also no reason to test numbers divisible by 2 or 5, since those are not coprime to 10 and therefore do not admit an A(n)A(n) at all.

For each admissible candidate, the implementation computes A(n)A(n) directly from the repunit remainder recurrence. Starting from the remainder of R(1)R(1), it repeatedly appends one more digit 1 modulo nn until the remainder becomes zero. The first candidate above one million whose period exceeds one million is the answer.

Pseudocode

Start from n = 10^6 + 1 and increase n one by one.

For each candidate n:
    Skip it if n shares a factor with 10.
    Compute A(n) by tracking the remainders of successive repunits modulo n:
        Begin with the remainder of R(1).
        Repeatedly append one more digit 1 modulo n until the remainder becomes 0.
        Count how many steps this takes.
    If A(n) is greater than 10^6, return n.

Complexity Analysis

  • Time: For each candidate nn, computing A(n)A(n) takes O(A(n))≤O(n)=O(106)O(A(n)) \leq O(n) = O(10^6) steps. The number of candidates checked before finding the answer is small (approximately 23, since the answer is 10000231000023). Total: O(106)O(10^6).
  • Space: O(1)O(1) — only a constant number of variables.

Answer

1000023\boxed{1000023}

Code

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

C++ project_euler/problem_129/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int A(int n) {
    // Find smallest k such that R(k) % n == 0
    // R(k) = (10*R(k-1) + 1)
    int r = 1;
    int k = 1;
    while (r != 0) {
        r = (10LL * r + 1) % n;
        k++;
    }
    return k;
}

int main() {
    int limit = 1000000;

    for (int n = limit + 1; ; n++) {
        if (n % 2 == 0 || n % 5 == 0) continue;
        if (A(n) > limit) {
            cout << n << endl;
            return 0;
        }
    }

    return 0;
}