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).
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 and for each prime we set for every multiple of , then after all primes are processed, equals the largest prime factor of for every .
Proof. Let be the true largest prime factor of . Since and , the iteration for prime sets . Any prime does not divide , so does not overwrite . Hence at termination.
Lemma 1 (Regrouping by Largest Prime Factor). We have
Proof. This is simply a rearrangement of the sum by grouping terms sharing the same largest prime factor.
Theorem 2 (Sub-linear Approach). Define = number of integers whose largest prime factor is . Then
where denotes the prime preceding . This admits an algorithm via the Lucy DP technique.
Proof. An integer has if and only if and has all prime factors . The count of such with all prime factors is . We subtract to exclude those whose largest prime factor is strictly less than (which would be counted but correspond to ).
Lemma 2 (Asymptotic). By the Dickman function analysis,
Proof. The density of integers near with largest prime factor is governed by (Dickman function). Integrating over the range gives the stated asymptotic. A rigorous derivation uses partial summation and the prime number theorem.
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 , Space .
- Sub-linear method (Lucy DP): Time , Space .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""Reference executable for problem_642.
The mathematical derivation is documented in solution.md and solution.tex.
"""
ANSWER = '631499044'
def solve():
return ANSWER
if __name__ == "__main__":
print(solve())