Discussion about this post

User's avatar
Sumanth's avatar

Ideas:

1. The range of divisors to check are contiguous floor(sqrt(L)) to floor(sqrt(R)).

let l = floor(sqrt(L)) and r = floor(sqrt(R))

2. Then all numbers from l+1 to r-1 have exactly 3 numbers they divide. We can get this by looking at the numbers they test in the range L to R, for any t in l to r. we need to check numbers in range [(t+1)^2 -1 , t^2]. Then count of numbers divisible by t in this range is floor(3 + 1/t) = 3

3. Finally we need to handle boundary cases with l and r:

For l : ((l+1)^2 - L)/l + 1

For r: (R - r^2)/r + 1

So the final answer will be

3*( r - l - 1) + ((l+1)^2 - L)/l +1 (add 1 if l is not 1) + (R - r^2)/r + 1

1 more comment...

No posts

Ready for more?