Project Euler Problem 521 Smallest prime factor - Solution

Smallest prime factor Problem 521 Let smpf( n ) be the smallest prime factor of n . smpf(91)=7 because 91=7×13 and smpf(45)=3 because 45=3×3×5. Let S( n ) be the sum of smpf( i ) for 2 ≤ i ≤ n . E.g. S(100)=1257. Find S(10 12 ) mod 10 9 . If you didn't already, read Hint 1 and Hint 2 first. Below is an implementation based on ideas from Hint 1 and Hint 2, with some performance tuning in place. Below c++ code reaches correct answer in ~1 hour. #include <iostream> #include <vector> using namespace std; int main () { const long long maxp = 1000000 ; const long long maxbillion = 100 ; const long long maxp2 = 10000000000 ; const long long maxarrsize = maxp2 + 1 ; const long long mod = 1000000000 ; std :: vector < bool > primemap( maxp + 1 , true ); std :: vector < long long > arrp = {}; long long sum1 = 0 ; for ( long ...