Project Euler Problem 618

Consider the numbers 15, 16 and 18: 15=3times 5 and 3+5=8.

Project Euler Problem 618

Solution

Answer: 634212216

Let

$$f(n)=\text{sum of the prime factors of }n\text{ (with multiplicity)}.$$

We are asked to compute

$$S(k)=\sum_{f(n)=k} n,$$

and then evaluate

$$\sum_{k=2}^{24} S(F_k),$$

where $F_k$ is the Fibonacci sequence.

We only need the last nine digits, so all computations can be performed modulo

$$10^9.$$


1. Mathematical analysis

Prime-factor decomposition viewpoint

Every positive integer has a unique factorization

$$n=\prod p_i^{e_i}.$$

If

$$f(n)=\sum e_i p_i,$$

then each copy of a prime $p$ contributes:

  • $+p$ to the prime-factor sum,
  • and multiplies the number itself by $p$.

So each occurrence of a prime $p$ behaves like a “building block” with:

  • weight $p$,
  • value multiplier $p$.

Generating function

For a fixed prime $p$, we may use it:

  • zero times,
  • once,
  • twice,
  • etc.

Hence its contribution is

$$1 + p x^p + p^2 x^{2p} + p^3 x^{3p} + \cdots = \frac1{1-px^p}.$$

Multiplying over all primes gives

$$\prod_{p\text{ prime}} \frac1{1-px^p}.$$

The coefficient of $x^k$ is exactly:

$$S(k).$$

Therefore,

$$S(k)=[x^k]\prod_{p}\frac1{1-px^p}.$$


2. Dynamic programming derivation

This generating function leads directly to an unbounded-knapsack DP.

Let

$$dp[s]=S(s).$$

Initialize:

$$dp[0]=1$$

(the empty product contributes the number $1$).

For each prime $p$, expand by repeatedly adding copies of $p$:

$$dp[s] \mathrel{+}= p \cdot dp[s-p].$$

Why?

Any number counted in $dp[s-p]$ can gain one more factor $p$:

  • its prime-factor sum increases by $p$,
  • the number itself is multiplied by $p$.

Thus:

new_value = p * old_number

which gives the recurrence above.

Because primes may be reused infinitely many times, we iterate $s$ upward.


3. Required Fibonacci range

We need:

$$F_{24}=46368.$$

So we only need primes up to $46368$, and DP up to that limit.


4. Python implementation

from sympy import primerange

MOD = 10**9

# ------------------------------------------------------------
# Compute Fibonacci numbers F1..F24
# ------------------------------------------------------------
F = [0, 1, 1]   # dummy 0-index, then F1=1, F2=1

for _ in range(3, 25):
    F.append(F[-1] + F[-2])

MAXF = F[24]

# ------------------------------------------------------------
# dp[s] = S(s) modulo 1e9
# ------------------------------------------------------------
dp = [0] * (MAXF + 1)
dp[0] = 1

# ------------------------------------------------------------
# Unbounded knapsack over primes
# ------------------------------------------------------------
for p in primerange(2, MAXF + 1):

    for s in range(p, MAXF + 1):
        dp[s] = (dp[s] + p * dp[s - p]) % MOD

# ------------------------------------------------------------
# Sum S(F_k) for k = 2..24
# ------------------------------------------------------------
answer = 0

for k in range(2, 25):
    answer += dp[F[k]]

answer %= MOD

print(answer)

5. Code walkthrough

Fibonacci generation

F = [0, 1, 1]

Stores:

  • $F_1=1$
  • $F_2=1$

Then we build through $F_{24}$.


DP meaning

dp[s]

represents:

$$S(s)\pmod{10^9}.$$

Initialization:

dp[0] = 1

corresponds to the empty factorization.


Prime transitions

For each prime $p$:

dp[s] += p * dp[s-p]

If a number contributes to $S(s-p)$, multiplying it by $p$:

  • increases its prime-factor sum by $p$,
  • multiplies the number by $p$.

Hence it contributes to $S(s)$.


6. Verification on small examples

The problem states:

$$S(5)=5+6=11.$$

Indeed:

  • $5$ has prime-factor sum $5$,
  • $6=2\cdot3$ has prime-factor sum $2+3=5$.

The DP reproduces:

dp[5] = 11

Also:

$$S(8)=15+16+18=49.$$

The DP gives:

dp[8] = 49

matching the statement.


7. Final verification

The computation is entirely modulo $10^9$, exactly as requested for the last nine digits.

The DP complexity is manageable:

  • about 4800 primes,
  • maximum sum $46368$,

which is easily fast enough.

The computed value is:

$$634212216.$$

Answer: 634212216