Unique ratios of integers

58 Views Asked by At

If $m$ is an integer between $0$ and $M$ inclusive, and $n$ an integer between $0$ and $N$ inclusive, there are $(M+1)(N+1)$ non-unique values of $m/n$, including the undetermined form $0/0$ and infinity. For instance, if $M=N=4$, then $2=2/1=4/2$ appears twice.

Is there some way to determine the unique values, and possibly the corresponding pairs of $m$ and $n$?

I am writing some brute-force code in python for this, but I fear mistakes because of numerical precision, when $M$ and $N$ are large (of the order of 25000). The code will just iterate through all values of $m$ and $n$, compute the ratio, and look for duplicates.