1

I have an algorithm to solve this problem * given two valid simplifying fractions $\dfrac{a}{b}$ and $\dfrac{c}{d},$ each transformation is adding both $a$ and $b$ by $+1,$ simplifying it, again and again until we meet $\dfrac{c}{d}.$ Count the number of steps used, $0$ if none" so that $\dfrac{a}{b}, \dfrac{c}{d}< 1$ and $0< a< b\leq 10^{5}, 0< c< d\leq 10^{5}.$ Easily, I see that the steps only continue if $ad< bc.$ Because we have that $\dfrac{a}{b}< \dfrac{a+ 1}{b+ 1}\overset{\div gcd(a, b)}{\rightarrow}\dfrac{a_{new}}{b_{new}}\rightarrow \dfrac{c}{d}.$ That sounds good.

But my test is not perfect, I wonder if there exist any $c, d$ so that $\dfrac{a_{new}}{b_{new}}\rightarrow \dfrac{c}{d}$ but never reaches $\dfrac{c}{d}.$ I need to your helps.

1 Answers1

1

Find $x$ such that $\frac{a+x}{b+x}=\frac cd$, i.e. $x=\frac{bc-ad}{d-c}$. If this is negative or not an integer, fail immediately. Otherwise, of all prime divisors $p$ of $b-a$, find the one minimising $y:=p-(b \bmod p)$. If $y\ge x$, the answer is $x$, otherwise it is $y$ plus the result for $\frac{a+y}{b+y}$ (simplified) abd$\frac cd$.