All Euler problems
Project Euler

Large Repunit Factors

A number consisting entirely of ones is called a repunit: R(k) = underbrace11ldots1_k = (10^k - 1)/(9). Find the sum of the first forty prime factors of R(10^9).

Source sync Apr 19, 2026
Problem #0132
Level Level 05
Solved By 7,326
Languages C++, Python
Answer 843296
Length 237 words
modular_arithmeticnumber_theorybrute_force

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(10) = 1111111111 = 11 \times 41 \times 271 \times 9091\), and the sum of these prime factors is \(9414\).

Find the sum of the first forty prime factors of \(R(10^9)\).

Problem 132: Large Repunit Factors

Mathematical Development

Theorem 1 (Divisibility Criterion). Let pp be a prime with p10p \nmid 10 and p3p \neq 3. Then pR(k)p \mid R(k) if and only if ordp(10)k\operatorname{ord}_p(10) \mid k, where ordp(10)\operatorname{ord}_p(10) denotes the multiplicative order of 1010 modulo pp.

Proof. Since gcd(10,p)=1\gcd(10, p) = 1 and gcd(9,p)=1\gcd(9, p) = 1 (as p3p \neq 3), we have the equivalence chain:

pR(k)    p10k19    p(10k1)    10k1(modp).p \mid R(k) \iff p \mid \frac{10^k - 1}{9} \iff p \mid (10^k - 1) \iff 10^k \equiv 1 \pmod{p}.

By the definition of multiplicative order, 10k1(modp)10^k \equiv 1 \pmod{p} if and only if ordp(10)k\operatorname{ord}_p(10) \mid k. \square

Lemma 1 (Divisor Structure of 10910^9). Since 109=295910^9 = 2^9 \cdot 5^9, a prime pp (with p30p \nmid 30) divides R(109)R(10^9) if and only if ordp(10)\operatorname{ord}_p(10) has the form 2a5b2^a \cdot 5^b with 0a90 \leq a \leq 9 and 0b90 \leq b \leq 9.

Proof. By Theorem 1, pR(109)p \mid R(10^9) iff ordp(10)109\operatorname{ord}_p(10) \mid 10^9. The divisors of 109=295910^9 = 2^9 \cdot 5^9 are precisely the integers 2a5b2^a \cdot 5^b with 0a90 \leq a \leq 9, 0b90 \leq b \leq 9. \square

Lemma 2 (Exclusion of Small Primes). The primes 22, 33, and 55 do not divide R(109)R(10^9).

Proof. Since R(k)R(k) consists entirely of the digit 11, it is odd (so 2R(k)2 \nmid R(k)) and its decimal representation ends in 11 (so 5R(k)5 \nmid R(k)).

For p=3p = 3: we have 3R(k)3 \mid R(k) iff 3k3 \mid k, since R(k)=10k19R(k) = \frac{10^k - 1}{9} and ord3(10)=1\operatorname{ord}_3(10) = 1 gives 3R(k)    93(10k1)    27(10k1)3 \mid R(k) \iff 9 \cdot 3 \mid (10^k - 1) \iff 27 \mid (10^k-1). Since ord27(10)=3\operatorname{ord}_{27}(10) = 3 (as 103=10001(mod27)10^3 = 1000 \equiv 1 \pmod{27}), this holds iff 3k3 \mid k. Now 31093 \nmid 10^9 (since gcd(3,10)=1\gcd(3, 10) = 1), so 3R(109)3 \nmid R(10^9). \square

Lemma 3 (Computational Test). The condition ordp(10)109\operatorname{ord}_p(10) \mid 10^9 is equivalent to 101091(modp)10^{10^9} \equiv 1 \pmod{p}, which can be verified in O(log(109))=O(30)O(\log(10^9)) = O(30) modular multiplications via binary exponentiation.

Proof. By definition, ordp(10)109\operatorname{ord}_p(10) \mid 10^9 iff 101091(modp)10^{10^9} \equiv 1 \pmod{p}. Binary exponentiation computes 10109modp10^{10^9} \bmod p using O(log2(109))30O(\lfloor \log_2(10^9) \rfloor) \leq 30 squaring and multiplication steps. \square

Editorial

A prime p (not 2, 3, or 5) divides R(K) iff 10^K = 1 (mod p). We iterate over p in primes.

Pseudocode

    K = 10^9
    primes = sieve_primes(B) // B ~ 200000 suffices empirically
    factors = []
    for p in primes:
        If p in {2, 3, 5} then continue
        If pow(10, K, p) == 1 then
            factors.append(p)
        If len(factors) == 40 then stop this loop
    Return sum(factors)

Complexity Analysis

  • Time: O(BloglogB+π(B)logK)O(B \log\log B + \pi(B) \cdot \log K), where B200,000B \approx 200{,}000 is the sieve bound, π(B)17,984\pi(B) \approx 17{,}984 is the number of primes up to BB, and logK30\log K \approx 30 bits for binary exponentiation. Concretely: O(BloglogB)O(B \log\log B) for the sieve plus O(π(B)30)O(\pi(B) \cdot 30) for the modular exponentiations.
  • Space: O(B)O(B) for the prime sieve.

Answer

843296\boxed{843296}

Code

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

C++ project_euler/problem_132/solution.cpp
/*
 * Project Euler Problem 132: Large Repunit Factors
 *
 * A prime p (not 2, 3, 5) divides R(10^9) iff 10^(10^9) = 1 (mod p).
 * Find the sum of the first 40 such primes.
 */
#include <bits/stdc++.h>
using namespace std;

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

int main() {
    const int LIMIT = 200000;
    vector<bool> is_prime(LIMIT, true);
    is_prime[0] = is_prime[1] = false;
    for (int i = 2; i * i < LIMIT; i++)
        if (is_prime[i])
            for (int j = i * i; j < LIMIT; j += i)
                is_prime[j] = false;

    long long K = 1000000000LL;
    long long total = 0;
    int count = 0;

    for (int p = 2; p < LIMIT && count < 40; p++) {
        if (!is_prime[p]) continue;
        if (p == 2 || p == 3 || p == 5) continue;
        if (power_mod(10, K, p) == 1) {
            total += p;
            count++;
        }
    }

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