Problem · Math

Count Lucky Numbers in a Range

Learn this problem
MediumTarget logoTargetFULLTIMEOA

Problem 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) → long

Examples

Example 1

l = 1r = 10return = 7

The lucky numbers are 1, 2, 3, 4, 6, 8, 9.

Example 2

l = 16r = 24return = 3

Throughout this interval floor(sqrt(x)) = 4. The divisible values are 16, 20, 24.

Example 3

l = 1000000000000000000r = 1000000000000000000return = 1

The endpoint is 10^18 = (10^9)^2, so it is divisible by its floored square root.

Constraints

  • 1 <= l <= r <= 10^18.

More Target problems

drafts saved locally
public long countLuckyNumbers(long l, long r) {
  // Write your code here.
}
l1
r10
expected7
checking account