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).
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 be a prime with and . Then if and only if , where denotes the multiplicative order of modulo .
Proof. Since and (as ), we have the equivalence chain:
By the definition of multiplicative order, if and only if .
Lemma 1 (Divisor Structure of ). Since , a prime (with ) divides if and only if has the form with and .
Proof. By Theorem 1, iff . The divisors of are precisely the integers with , .
Lemma 2 (Exclusion of Small Primes). The primes , , and do not divide .
Proof. Since consists entirely of the digit , it is odd (so ) and its decimal representation ends in (so ).
For : we have iff , since and gives . Since (as ), this holds iff . Now (since ), so .
Lemma 3 (Computational Test). The condition is equivalent to , which can be verified in modular multiplications via binary exponentiation.
Proof. By definition, iff . Binary exponentiation computes using squaring and multiplication steps.
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: , where is the sieve bound, is the number of primes up to , and bits for binary exponentiation. Concretely: for the sieve plus for the modular exponentiations.
- Space: for the prime sieve.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
/*
* 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;
}
"""
Project Euler Problem 132: Large Repunit Factors
Find the sum of the first 40 prime factors of R(10^9).
A prime p (not 2, 3, or 5) divides R(K) iff 10^K = 1 (mod p).
"""
def sieve(limit):
is_prime = [True] * limit
is_prime[0] = is_prime[1] = False
for i in range(2, int(limit**0.5) + 1):
if is_prime[i]:
for j in range(i * i, limit, i):
is_prime[j] = False
return [i for i in range(2, limit) if is_prime[i]]
K = 10**9
primes = sieve(200000)
factors = []
for p in primes:
if p in (2, 3, 5):
continue
if pow(10, K, p) == 1:
factors.append(p)
if len(factors) == 40:
break
print(sum(factors))