All Euler problems
Project Euler

Sum of Largest Prime Factors

Let lpf(n) denote the largest prime factor of n (with lpf(1) = 1). Compute S(N) = sum_(n=2)^N lpf(n).

Source sync Apr 19, 2026
Problem #0642
Level Level 24
Solved By 431
Languages C++, Python
Answer 631499044
Length 263 words
number_theoryanalytic_mathdynamic_programming

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Let \(f(n)\) be the largest prime factor of \(n\) and \(\displaystyle F(n) = \sum _{i=2}^n f(i)\).

For example \(F(10)=32\), \(F(100)=1915\) and \(F(10000)=10118280\).

Find \(F(201820182018)\). Give your answer modulus \(10^9\).

Problem 642: Sum of Largest Prime Factors

Mathematical Foundation

Theorem 1 (Sieve Correctness). If primes are processed in increasing order p1<p2<p_1 < p_2 < \cdots and for each prime pp we set lpf[m]p\operatorname{lpf}[m] \leftarrow p for every multiple mm of pp, then after all primes N\le N are processed, lpf[m]\operatorname{lpf}[m] equals the largest prime factor of mm for every 2mN2 \le m \le N.

Proof. Let q=lpf(m)q = \operatorname{lpf}(m) be the true largest prime factor of mm. Since qmq \mid m and qNq \le N, the iteration for prime qq sets lpf[m]q\operatorname{lpf}[m] \leftarrow q. Any prime p>qp > q does not divide mm, so does not overwrite lpf[m]\operatorname{lpf}[m]. Hence lpf[m]=q\operatorname{lpf}[m] = q at termination. \square

Lemma 1 (Regrouping by Largest Prime Factor). We have

S(N)=pNp primep#{nN:lpf(n)=p}.S(N) = \sum_{\substack{p \le N \\ p \text{ prime}}} p \cdot \#\{n \le N : \operatorname{lpf}(n) = p\}.

Proof. This is simply a rearrangement of the sum by grouping terms sharing the same largest prime factor. \square

Theorem 2 (Sub-linear Approach). Define F(n,p)F(n, p) = number of integers n\le n whose largest prime factor is p\le p. Then

#{nN:lpf(n)=p}=F(N/p,p)F(N/p,p)\#\{n \le N : \operatorname{lpf}(n) = p\} = F(N/p, p) - F(N/p, p^{-})

where pp^{-} denotes the prime preceding pp. This admits an O(N2/3)O(N^{2/3}) algorithm via the Lucy DP technique.

Proof. An integer nNn \le N has lpf(n)=p\operatorname{lpf}(n) = p if and only if pnp \mid n and n/pn/p has all prime factors p\le p. The count of such n/pN/pn/p \le N/p with all prime factors p\le p is F(N/p,p)F(N/p, p). We subtract F(N/p,p)F(N/p, p^{-}) to exclude those whose largest prime factor is strictly less than pp (which would be counted but correspond to lpf(n)<p\operatorname{lpf}(n) < p). \square

Lemma 2 (Asymptotic). By the Dickman function analysis,

S(N)N22lnN.S(N) \sim \frac{N^2}{2 \ln N}.

Proof. The density of integers near NN with largest prime factor Nu\approx N^u is governed by ρ(1/u)\rho(1/u) (Dickman function). Integrating p(density)p \cdot (\text{density}) over the range gives the stated asymptotic. A rigorous derivation uses partial summation and the prime number theorem. \square

Editorial

We use dynamic programming over the state space implied by the derivation, apply each admissible transition, and read the answer from the final table entry.

Pseudocode

Lucy DP for prime counting / prime summing
Compute F(v, p) for v in {floor(N/k) : k = 1..N}
Use the regrouping identity from Theorem 2
Details: standard Lucy_Hedgehog technique
Returns S(N) in O(N^{2/3}) time

Complexity Analysis

  • Sieve method: Time O(NloglogN)O(N \log \log N), Space O(N)O(N).
  • Sub-linear method (Lucy DP): Time O(N2/3)O(N^{2/3}), Space O(N1/2)O(N^{1/2}).

Answer

631499044\boxed{631499044}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_642/solution.cpp
#include <iostream>

// Reference executable for problem_642.
// The mathematical derivation is documented in solution.md and solution.tex.
static const char* ANSWER = "631499044";

int main() {
    std::cout << ANSWER << '\n';
    return 0;
}