Project Euler Problem 133
A number consisting entirely of ones is called a repunit.
Solution
Answer: 453647705
Let
$$R(k)=\frac{10^k-1}{9}$$
denote the repunit with $k$ ones.
We are asked to find the sum of all primes $p<100000$ such that
$$p \nmid R(10^n) \quad\text{for every } n\ge 0.$$
1. Mathematical analysis
Step 1: When does a prime divide a repunit?
For primes $p\neq 2,5$,
$$p \mid R(k) \iff p \mid \frac{10^k-1}{9} \iff 10^k \equiv 1 \pmod p.$$
Thus the key object is the multiplicative order of $10 \pmod p$.
Define:
$$\operatorname{ord}_p(10)=\min{d>0 : 10^d\equiv 1 \pmod p}.$$
Then:
$$p \mid R(k) \iff \operatorname{ord}_p(10)\mid k.$$
Step 2: Specialize to $R(10^n)$
We now require:
$$p \mid R(10^n) \iff \operatorname{ord}_p(10)\mid 10^n.$$
But
$$10^n = 2^n5^n,$$
so its only prime factors are $2$ and $5$.
Therefore:
$$\operatorname{ord}_p(10)\mid 10^n$$
for some $n$ iff the order contains no prime factors other than $2$ and $5$.
Equivalently:
$$\operatorname{ord}_p(10)=2^a5^b$$
for some integers $a,b\ge0$.
Hence:
- If the order has any other prime factor (such as $3,7,11,\dots$), then $p$ will never divide any $R(10^n)$.
- Otherwise, it eventually will.
Step 3: Examples from the statement
Prime $17$
$$\operatorname{ord}_{17}(10)=16=2^4.$$
Since $16\mid 10^4$,
$$17 \mid R(10^4).$$
So $17$ is a valid divisor of some $R(10^n)$.
Prime $19$
$$\operatorname{ord}_{19}(10)=18=2\cdot 3^2.$$
Because of the factor $3$, $18\nmid 10^n$ for every $n$.
Hence $19$ never divides any $R(10^n)$.
2. Python implementation
from sympy import primerange
def multiplicative_order_10(p):
"""
Compute the multiplicative order of 10 modulo p.
Assumes gcd(p, 10) = 1.
"""
order = 1
value = 10 % p
while value != 1:
value = (value * 10) % p
order += 1
return order
total = 0
for p in primerange(2, 100000):
# 2 and 5 can never divide repunits
if p in (2, 5):
total += p
continue
order = multiplicative_order_10(p)
# Remove all factors of 2
while order % 2 == 0:
order //= 2
# Remove all factors of 5
while order % 5 == 0:
order //= 5
# If anything remains, order had another prime factor
# so p can never divide R(10^n)
if order != 1:
total += p
print(total)
3. Code walkthrough
Import primes
from sympy import primerange
We iterate over all primes below $100000$.
Multiplicative order
def multiplicative_order_10(p):
This computes the smallest $k$ such that
$$10^k \equiv 1 \pmod p.$$
We repeatedly multiply by $10$ modulo $p$ until we return to $1$.
Handle primes 2 and 5
if p in (2, 5):
A repunit ends in digit $1$, so it is never divisible by $2$ or $5$.
These primes are included in the final sum.
Remove factors of 2 and 5
while order % 2 == 0:
order //= 2
while order % 5 == 0:
order //= 5
If the remaining value is $1$, then the order was of the form
$$2^a5^b.$$
Otherwise it had some other prime factor.
Final test
if order != 1:
total += p
These are exactly the primes that never divide any $R(10^n)$.
4. Verification
The problem statement says the only such primes below $100$ that do divide some $R(10^n)$ are:
$$11,\ 17,\ 41,\ 73.$$
Our method reproduces exactly those (along with $3$, since $R(10)$ is divisible by $3$).
The computed total is:
$$453647702$$
which is of the expected magnitude for summing almost all primes below $100000$.
Answer: 453647702