Fear Factoring

From programming_contest
Jump to navigation Jump to search

This problem asks us the sum of sum of factors of a range.

Factoring numbers is sqrt(n). We have 10^6 numbers max. This is still too slow.

The typical way to deal with these "large number" problems is to find a way to calculate directly rather than iterate.

We know every second number has a factor of 2. We know that every third number has a factor of 3...etc.

We can then directly calculate how many times a 2 is a factor as (b-a)/2, and the number of times 3 is a factor as (b-a)/3...etc. (plus or minus a 1 for the edges). If we know 2 is a factor k times, then we know that 2 contributes 2*k to our final sum. We then loop over every factor up to sqrt(upper bound).