Problem 551
Divisor Summatory Function
Hard · 30 pts · Math, Number Theory
Problem
Let d(i) be the number of positive divisors of i. Compute D(N) = d(1) + d(2) + ... + d(N). Each number k divides exactly floor(N / k) of the numbers 1..N, so D(N) is also the sum of floor(N / k) for k = 1..N. With N up to 10^12 even that loop is too slow. Use the symmetry of divisor pairs: with s = floor(√N), D(N) = 2 × (floor(N/1) + ... + floor(N/s)) - s². The numbers in this problem can be larger than a 32-bit int can hold (about 2.1 × 10^9), so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).
Input
A single integer N.
Output
Print D(N).
Constraints
1 <= N <= 10^12
Example input
5
Example output
10
Explanation
For N = 5 the divisor counts are d(1) = 1, d(2) = 2, d(3) = 2, d(4) = 3 and d(5) = 2. Their sum is 10.