All Euler problems
Project Euler

Repunit Nonfactors

A repunit R(k) = (10^k - 1)/(9). Find the sum of all primes below 100,000 that will never be a factor of R(10^n) for any positive integer n.

Source sync Apr 19, 2026
Problem #0133
Level Level 06
Solved By 6,431
Languages C++, Python
Answer 453647705
Length 256 words
modular_arithmeticnumber_theorylinear_algebra

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\).

Let us consider repunits of the form \(R(10^n)\).

Although \(R(10)\), \(R(100)\), or \(R(1000)\) are not divisible by \(17\), \(R(10000)\) is divisible by \(17\). Yet there is no value of \(n\) for which \(R(10^n)\) will divide by \(19\). In fact, it is remarkable that \(11\), \(17\), \(41\), and \(73\) are the only four primes below one-hundred that can be a factor of \(R(10^n)\).

Find the sum of all the primes below one-hundred thousand that will never be a factor of \(R(10^n)\).

Problem 133: Repunit Nonfactors

Mathematical Development

Theorem 1 (5-Smooth Order Criterion). For a prime pp with p10p \nmid 10 and p3p \neq 3, there exists a positive integer nn such that pR(10n)p \mid R(10^n) if and only if ordp(10)\operatorname{ord}_p(10) is 5-smooth (i.e., all prime factors of ordp(10)\operatorname{ord}_p(10) lie in {2,5}\{2, 5\}).

Proof. By the divisibility theory for repunits (Theorem 1 of Problem 132), pR(k)p \mid R(k) iff ordp(10)k\operatorname{ord}_p(10) \mid k. Therefore:

(n1:pR(10n))    (n1:ordp(10)10n).\bigl(\exists\, n \geq 1 : p \mid R(10^n)\bigr) \iff \bigl(\exists\, n \geq 1 : \operatorname{ord}_p(10) \mid 10^n\bigr).

(\Rightarrow) If ordp(10)10n=2n5n\operatorname{ord}_p(10) \mid 10^n = 2^n \cdot 5^n for some nn, then every prime factor of ordp(10)\operatorname{ord}_p(10) divides 2n5n2^n \cdot 5^n, so ordp(10)\operatorname{ord}_p(10) is 5-smooth.

(\Leftarrow) If ordp(10)=2a5b\operatorname{ord}_p(10) = 2^a \cdot 5^b, take n=max(a,b)n = \max(a, b). Then 10n=2n5n10^n = 2^n \cdot 5^n is divisible by 2a5b=ordp(10)2^a \cdot 5^b = \operatorname{ord}_p(10), so pR(10n)p \mid R(10^n). \square

Lemma 1 (Permanent Nonfactors). The primes p=2p = 2, p=3p = 3, and p=5p = 5 never divide R(10n)R(10^n) for any n1n \geq 1.

Proof. The repunit R(k)=111kR(k) = \underbrace{11\ldots1}_k is odd (so 2R(k)2 \nmid R(k)) and ends in digit 1 (so 5R(k)5 \nmid R(k)), for all k1k \geq 1.

For p=3p = 3: since ord27(10)=3\operatorname{ord}_{27}(10) = 3 (verifiable: 103=10001(mod27)10^3 = 1000 \equiv 1 \pmod{27}), we have 3R(k)    3k3 \mid R(k) \iff 3 \mid k. Since gcd(3,10)=1\gcd(3, 10) = 1 implies 310n3 \nmid 10^n for all nn, the prime 3 never divides R(10n)R(10^n). \square

Lemma 2 (Efficient Testing). For p<105p < 10^5, the condition ”ordp(10)\operatorname{ord}_p(10) is 5-smooth” is equivalent to 1010201(modp)10^{10^{20}} \equiv 1 \pmod{p}, where 1020=22052010^{20} = 2^{20} \cdot 5^{20}.

Proof. Since ordp(10)p1<105\operatorname{ord}_p(10) \mid p - 1 < 10^5, any 5-smooth divisor of p1p - 1 is of the form 2a5b2^a \cdot 5^b with alog2(105)=16a \leq \lfloor \log_2(10^5) \rfloor = 16 and blog5(105)=7b \leq \lfloor \log_5(10^5) \rfloor = 7. Hence ordp(10)220520=1020\operatorname{ord}_p(10) \mid 2^{20} \cdot 5^{20} = 10^{20}, and the claim follows from the definition of multiplicative order.

Conversely, if 1010201(modp)10^{10^{20}} \equiv 1 \pmod{p}, then ordp(10)1020\operatorname{ord}_p(10) \mid 10^{20}, so ordp(10)\operatorname{ord}_p(10) is 5-smooth. \square

Remark. An equivalent approach computes ordp(10)\operatorname{ord}_p(10) explicitly by factoring p1p - 1 and refining, then checks 5-smoothness by dividing out all factors of 2 and 5.

Editorial

A prime p (not 2, 3, 5) can divide some R(10^n) iff ord_p(10) is 5-smooth. We iterate over p in primes.

Pseudocode

    N = 100000
    primes = sieve_primes(N)
    total = 0
    for p in primes:
        If p in {2, 3, 5} then
            total += p // permanent nonfactors
            continue
        d = multiplicative_order(10, p)
        If not is_5_smooth(d) then
            total += p // nonfactor
    Return total

Complexity Analysis

  • Time: O(NloglogN)O(N \log\log N) for the sieve. For each prime pp, computing ordp(10)\operatorname{ord}_p(10) requires factoring p1p - 1 in O(p)O(\sqrt{p}) time and O(logp)O(\log p) modular exponentiations. Over all π(N)\pi(N) primes, the total is O(N3/2/logN)O(N^{3/2} / \log N) in the worst case, though typically much faster.
  • Space: O(N)O(N) for the sieve.

Answer

453647705\boxed{453647705}

Code

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

C++ project_euler/problem_133/solution.cpp
/*
 * Project Euler Problem 133: Repunit Nonfactors
 *
 * Sum of all primes below 100000 that never divide R(10^n).
 * A prime p can divide some R(10^n) iff ord_p(10) is 5-smooth.
 */
#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 = 100000;
    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 total = 0;

    for (int p = 2; p < LIMIT; p++) {
        if (!is_prime[p]) continue;
        if (p == 2 || p == 3 || p == 5) {
            total += p;
            continue;
        }

        // Compute ord_p(10) by factoring p-1 and refining
        int pm1 = p - 1;
        vector<int> factors;
        int temp = pm1;
        for (int f = 2; (long long)f * f <= temp; f++) {
            if (temp % f == 0) {
                factors.push_back(f);
                while (temp % f == 0) temp /= f;
            }
        }
        if (temp > 1) factors.push_back(temp);

        int d = pm1;
        for (int f : factors)
            while (d % f == 0 && power_mod(10, d / f, p) == 1)
                d /= f;

        // Check if d is 5-smooth
        int dd = d;
        while (dd % 2 == 0) dd /= 2;
        while (dd % 5 == 0) dd /= 5;

        if (dd != 1)
            total += p;
    }

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