Problem · Math
Count Lucky Numbers in a Range
Learn this problemProblem statement
An integer x is lucky when it is divisible by floor(sqrt(x)).
Given two integers l and r, return the number of lucky integers in the inclusive interval [l, r].
Implement countLuckyNumbers(l, r).
Function
countLuckyNumbers(l: long, r: long) → longExamples
Example 1
l = 1r = 10return = 7The lucky numbers are 1, 2, 3, 4, 6, 8, 9.
Example 2
l = 16r = 24return = 3Throughout this interval floor(sqrt(x)) = 4. The divisible values are 16, 20, 24.
Example 3
l = 1000000000000000000r = 1000000000000000000return = 1The endpoint is 10^18 = (10^9)^2, so it is divisible by its floored square root.
Constraints
1 <= l <= r <= 10^18.