0
$\begingroup$

Given $1 \leq a \leq b \leq n^{2}$, how many perfect squares divisible by $m$, where $1 \leq m \leq n$ exist in the interval $[a,b]$. This number be computed by doing a linear search. Can this be done any faster (say in time $log_{2}(b - a)$?

1 Answers 1

1

Write $m=k^2n$ with $n$ square free.

Then, if $x$ is a perfect square divisible by $m$, $x$ must have the form $$x=mny^2$$

Therefore the problem becomes: How many perfect squares $y^2$ exists between $\frac{a}{mn}$ and $\frac{b}{mn}$. This is equivalent to figuring how many integers $y$ exists in the interval $[\sqrt{\frac{a}{mn}}, \sqrt{\frac{b}{mn}}]$.

So, as long as you can find the prime factorisation of $m$, the answer can be calculated without any search.