Project Euler Problem 168
Consider the number 142857.
Solution
Answer: 59206
Let
$$n = 10a+b$$
in decimal notation, where $b$ is the last digit.
Right-rotating $n$ moves $b$ to the front:
$$R(n)=b\cdot 10^{k-1}+a$$
where $k$ is the number of digits of $n$.
We are told that
$$R(n)=m n$$
for some integer multiplier $m$.
We seek all $n$ with
$$10<n<10^{100}$$
satisfying this property, and we want the last $5$ digits of their sum.
Mathematical analysis
Suppose $n$ has $k$ digits.
Write
$$n=10a+b,$$
where:
- $b\in{1,\dots,9}$ (the last digit cannot be $0$, otherwise the rotation loses a digit),
- $0\le a<10^{k-1}$.
The rotated number is
$$R(n)=b\cdot 10^{k-1}+a.$$
The condition is
$$b\cdot 10^{k-1}+a = m(10a+b).$$
Rearrange:
$$b\cdot 10^{k-1}+a = 10ma+mb,$$
so
$$b(10^{k-1}-m)=a(10m-1).$$
Hence
$$a=\frac{b(10^{k-1}-m)}{10m-1}.$$
Then
$$n=10a+b$$
becomes
$$n =10\cdot \frac{b(10^{k-1}-m)}{10m-1}+b.$$
Combine terms:
$$n = \frac{10b(10^{k-1}-m)+b(10m-1)}{10m-1}.$$
The $-10bm$ and $+10bm$ cancel:
$$n = \frac{b(10^k-1)}{10m-1}.$$
So every solution must have the form
$$\boxed{ n=\frac{b(10^k-1)}{10m-1} }$$
with:
- $1\le b\le 9$,
- $1\le m\le 9$,
- denominator dividing numerator.
Key divisibility condition
We need
$$10m-1 \mid b(10^k-1).$$
Since
$$10m-1 \in {9,19,29,39,49,59,69,79,89},$$
we can analyze the multiplicative order of $10$ modulo these numbers.
For each $m$, define
$$d=10m-1.$$
If
$$10^k\equiv 1 \pmod d,$$
then $d\mid (10^k-1)$, and every digit $b$ works.
Otherwise only some $b$ may compensate for the missing factors.
Because $k<100$, we can brute force all:
- $k=2,\dots,99$,
- $m=1,\dots,9$,
- $b=1,\dots,9$,
and test divisibility.
The total search space is tiny:
$$98\times 9\times 9 = 7938.$$
Python implementation
MOD = 100000
total = 0
# k = number of digits
for k in range(2, 100):
ten_k_minus_1 = pow(10, k) - 1
# multiplier m
for m in range(1, 10):
d = 10 * m - 1
# final digit b
for b in range(1, 10):
numerator = b * ten_k_minus_1
# n must be integer
if numerator % d == 0:
n = numerator // d
# verify the property directly
s = str(n)
rotated = int(s[-1] + s[:-1])
if rotated == m * n:
total = (total + n) % MOD
print(total)
Code walkthrough
MOD = 100000
total = 0
We only need the last $5$ digits, so we maintain the sum modulo $100000$.
for k in range(2, 100):
The problem requires
$$10<n<10^{100},$$
so $n$ can have from $2$ to $99$ digits.
ten_k_minus_1 = pow(10, k) - 1
This computes
$$10^k-1,$$
the repeating-9 number.
for m in range(1, 10):
The rotation cannot increase the number by more than a factor of $9$, so $m\in{1,\dots,9}$.
d = 10 * m - 1
From the derived formula:
$$n=\frac{b(10^k-1)}{10m-1}.$$
for b in range(1, 10):
The last digit cannot be $0$.
numerator = b * ten_k_minus_1
if numerator % d == 0:
This checks whether
$$n = \frac{b(10^k-1)}{10m-1}$$
is an integer.
n = numerator // d
Construct the candidate.
s = str(n)
rotated = int(s[-1] + s[:-1])
Perform the right rotation explicitly.
if rotated == m * n:
Verify the defining property exactly.
total = (total + n) % MOD
Accumulate only the last five digits.
Small example verification
The problem example is:
$$142857$$
Right rotation:
$$714285.$$
Indeed:
$$714285 = 5 \times 142857.$$
Using the formula:
- $k=6$,
- $m=5$,
- $b=7$,
we get
$$n=\frac{7(10^6-1)}{49} =\frac{7\cdot 999999}{49} =142857.$$
So the derivation matches the example perfectly.
Final verification
The search space is exhaustive:
- every valid number must satisfy the derived formula,
- all $k<100$, $m\in[1,9]$, and $b\in[1,9]$ are tested,
- the defining property is verified directly.
Running the program gives:
$$59206$$
Thus the last five digits of the required sum are:
Answer: 59206