I am given $A$ and $B$, where $A$ is less than or equal to $B$, and they are in the range of $[1, 100000000]$. I want to calculate the number of pronic numbers in that interval $[A, B]$. These numbers are numbers $n$ such that $k \times (k + 1) = n$ for some $k$. The following algorithm works, but I am having a lot of trouble understanding the intuition behind it. Can one explain to me why it works?
def pronic(num) :
# Check upto sqrt N
N = int(num ** (1/2));
# If product of consecutive
# numbers are less than equal to num
if (N * (N + 1) <= num) :
return N;
# Return N - 1
return N - 1;
# Function to count pronic
# numbers in the range [A, B]
def countPronic(A, B) :
# Subtract the count of pronic numbers
# which are <= (A - 1) from the count
# f pronic numbers which are <= B
return pronic(B) - pronic(A - 1);