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 Apr 19, 2026
Problem #0129
Level Level 05
Solved By 7,283
Languages C++, Python
Answer 1000023
Length 275 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 Foundation

Definition. The repunit of length kk is R(k)=10k19R(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)modnr_k = R(k) \bmod n for k=1,2,,n+1k = 1, 2, \ldots, n+1. This sequence takes values in {0,1,,n1}\{0, 1, \ldots, n-1\}.

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

Case 2: If rk0r_k \neq 0 for all knk \leq n, then by the pigeonhole principle, there exist 1i<jn+11 \leq i < j \leq n+1 with ri=rjr_i = r_j. Now:

R(j)R(i)=111j111i=111ji10i=R(ji)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 nR(ji)n \mid R(j-i), where 1jin1 \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 9n9 \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)=(10k1)/9R(k) = (10^k - 1)/9. If gcd(n,9)=1\gcd(n, 9) = 1, then nR(k)n(10k1)10k1(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 3n3 \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=1modnr_1 = 1 \bmod n, rk=(10rk1+1)modnr_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)=10R(k1)+1R(k) = 10 \cdot R(k-1) + 1. Working modulo nn preserves the recurrence: rkR(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

We compute A(n). We then check after loop: need to handle k=1 case (r=1 != 0 for n>1). Finally, actually iterate: r starts at 1 (=R(1)), check if 0, then update.

Pseudocode

Compute A(n)
Check after loop: need to handle k=1 case (r=1 != 0 for n>1)
Actually iterate: r starts at 1 (=R(1)), check if 0, then update

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;
}