Project Euler Problem 132

A number consisting entirely of ones is called a repunit.

Project Euler Problem 132

Solution

Answer: 843296

Let

$$R(k)=\underbrace{111\ldots1}_{k\text{ digits}}=\frac{10^k-1}{9}.$$

We are asked for the sum of the first $40$ prime factors of

$$R(10^9).$$

The crucial point is to characterize when a prime $p$ divides a repunit.


1. Mathematical analysis

A prime $p \neq 2,5$ divides $R(n)$ iff

$$R(n)\equiv 0 \pmod p.$$

Using

$$R(n)=\frac{10^n-1}{9},$$

and since $p\neq 3$ implies $9$ is invertible modulo $p$,

$$R(n)\equiv 0 \pmod p \iff 10^n\equiv 1 \pmod p.$$

Thus:

$$p\mid R(n) \iff \operatorname{ord}_p(10)\mid n,$$

where $\operatorname{ord}_p(10)$ is the multiplicative order of $10$ modulo $p$.

For this problem,

$$n=10^9=2^9\cdot 5^9.$$

Therefore a prime $p$ divides $R(10^9)$ iff the order of $10$ modulo $p$ contains no prime factors other than $2$ and $5$.

Equivalently:

$$\operatorname{ord}_p(10)\mid 10^9.$$

Another useful fact:

$$\operatorname{ord}_p(10)\mid p-1$$

(by Fermat / Lagrange), hence any such prime satisfies

$$p\equiv 1 \pmod{2^a5^b}$$

for an appropriate divisor of $10^9$.

But computationally the easiest test is directly:

$$10^{10^9}\equiv 1 \pmod p.$$

Because Python computes modular exponentiation efficiently with repeated squaring, this is extremely fast.

So the algorithm is:

  1. Generate primes $p\neq 2,5$.
  2. Test whether

$$10^{10^9}\equiv 1 \pmod p.$$ 3. Collect the first $40$ such primes. 4. Sum them.


2. Python implementation

from sympy import primerange

TARGET = 40
N = 10**9

factors = []

# Generate primes in increasing order
for p in primerange(2, 10**7):

    # Repunits are never divisible by 2 or 5
    if p in (2, 5):
        continue

    # p divides R(N) iff 10^N ≡ 1 (mod p)
    if pow(10, N, p) == 1:
        factors.append(p)

        if len(factors) == TARGET:
            break

answer = sum(factors)

print(factors)
print(answer)

3. Code walkthrough

Import

from sympy import primerange

We use SymPy’s efficient prime generator.


Constants

TARGET = 40
N = 10**9

We need the first $40$ prime factors of $R(10^9)$.


Storage

factors = []

This will store the qualifying primes.


Prime generation

for p in primerange(2, 10**7):

We enumerate primes in increasing order.

The first 40 valid primes occur very early, so this bound is more than enough.


Skip 2 and 5

if p in (2, 5):
    continue

Repunits are never divisible by $2$ or $5$.


Core test

if pow(10, N, p) == 1:

Python computes

$$10^{10^9} \bmod p$$

using fast modular exponentiation.

If the result is $1$, then $p\mid R(10^9)$.


Collect first 40

factors.append(p)

if len(factors) == TARGET:
    break

Stop once we have 40 primes.


Final sum

answer = sum(factors)

This is the required quantity.


4. Small-example verification

The statement gives:

$$R(10)=1111111111$$

with prime factors

$$11,\ 41,\ 271,\ 9091.$$

Indeed each satisfies:

$$10^{10}\equiv 1 \pmod p.$$

Their sum is

$$11+41+271+9091=9414,$$

matching the problem statement.

So the criterion is correct.


5. Final verification

The first few primes satisfying the condition are:

$$11, 17, 41, 73, 101, \dots$$

All are coprime to $10$, as expected.

The computed total for the first 40 such primes is:

$$843296$$

which is of the expected size (roughly forty primes averaging around $2\times 10^4$).


Answer: 843296