Editor

Sum of Floor Divisions

#437 · Math · Expert · 200 XP · about 30 min

Write floor_sum(n) that returns n // 1 + n // 2 + n // 3 + ... + n // n.

The sum has a nice meaning: n // i is how many numbers from 1 to n are multiples of i, so the total counts every divisor of every number from 1 to n. For n = 5, the numbers 1, 2, 3, 4 and 5 have 1, 2, 2, 3 and 2 divisors, which makes 10, and 5 + 2 + 1 + 1 + 1 is 10 as well.

Examples

floor_sum(5)     # returns 10
floor_sum(10)    # returns 27
floor_sum(1)     # returns 1

Constraints

  • 1 <= n <= 10^12
  • With n close to a trillion, the n terms cannot be added one at a time.

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.