Given positive integers $a, b, D$. How to find $x, y \in \mathbb{Z^+}$ such that $$M =\left | \frac ab -\frac xy \right |$$ is minimal and $x + y \le D$?
For a solution, I can get it by brute-force but I can't find efficient way to solve this problem.