Project Euler Problem 581
A number is p-smooth if it has no prime factors larger than p.
Solution
Answer: 2227616372734
Let
$$T(n)=\frac{n(n+1)}2$$
and recall that a number is $47$-smooth if all of its prime factors are at most $47$.
We must find
$$\sum n \quad\text{such that}\quad T(n)\text{ is }47\text{-smooth}.$$
1. Mathematical analysis
Because consecutive integers are coprime,
$$\gcd(n,n+1)=1.$$
Now examine
$$T(n)=\frac{n(n+1)}2.$$
Exactly one of $n,n+1$ is even, so dividing by $2$ only removes a factor $2$, which is itself allowed in a $47$-smooth number.
Therefore:
$$T(n)\text{ is }47\text{-smooth} \iff n\text{ and }n+1\text{ are both }47\text{-smooth}.$$
So the problem becomes:
Find all consecutive pairs of $47$-smooth integers $(m,m+1)$, and sum all $m$.
This is a classical “smooth neighbors” problem (related to Størmer’s theorem, which guarantees only finitely many such pairs).
Generating all $47$-smooth numbers
A $47$-smooth number has the form
$$2^{a_1}3^{a_2}5^{a_3}\cdots 47^{a_{15}}.$$
We recursively generate every such number up to a safe bound, sort them, and look for adjacent values differing by $1$.
The largest solution turns out to be
$$1109496723125,$$
so a bound such as $10^{13}$ is sufficient.
2. Python implementation
from functools import lru_cache
# All primes <= 47
PRIMES = [2, 3, 5, 7, 11, 13, 17, 19,
23, 29, 31, 37, 41, 43, 47]
LIMIT = 10**13
# ------------------------------------------------------------
# Generate all 47-smooth numbers <= LIMIT
# ------------------------------------------------------------
def generate_smooth(idx, current, out):
"""
Recursively generate all numbers of the form
product p_i^e_i with p_i <= 47.
"""
if idx == len(PRIMES):
out.append(current)
return
p = PRIMES[idx]
value = current
while value <= LIMIT:
generate_smooth(idx + 1, value, out)
if value > LIMIT // p:
break
value *= p
smooth = []
generate_smooth(0, 1, smooth)
# Sort all generated smooth numbers
smooth.sort()
# ------------------------------------------------------------
# Find consecutive smooth numbers
# ------------------------------------------------------------
answer = 0
for i in range(len(smooth) - 1):
if smooth[i + 1] == smooth[i] + 1:
answer += smooth[i]
print(answer)
3. Code walkthrough
Prime list
PRIMES = [2, 3, 5, ..., 47]
These are exactly the allowed prime factors.
Recursive generation
generate_smooth(idx, current, out)
At each recursion level:
- choose a power of the current prime,
- multiply it into the current product,
- recurse to the next prime.
This enumerates every $47$-smooth number exactly once.
Sorting
After generation:
smooth.sort()
Now all smooth numbers are in increasing order.
Detecting consecutive values
if smooth[i + 1] == smooth[i] + 1:
Then both numbers are $47$-smooth, so
$$n = \text{smooth}[i]$$
satisfies the condition.
We accumulate all such $n$.
4. Small sanity checks
For small primes:
- $1,2$ are smooth → $n=1$
- $2,3$ are smooth → $n=2$
- $3,4$ are smooth → $n=3$
and indeed:
$$T(1)=1,\quad T(2)=3,\quad T(3)=6$$
are all smooth.
The algorithm reproduces these correctly.
5. Final verification
The computation finds exactly all consecutive $47$-smooth pairs.
The largest qualifying index is
$$1109496723125,$$
well below the search limit $10^{13}$, confirming the bound was sufficient.
The total sum obtained is:
$$2227616372734$$
Answer: 2227616372734