0

I'm trying to create a computer algebra package which stores numbers as fractions instead of decimals. To prevent the denominators from getting too big in the middle of a long computation, the idea is to substitute from time to time a given fraction with an approximation whose denominator does not exceed a certain fixed bound. The lazy option is to use the fixed bound itself as the denominator(if the original fraction in reduced form has a denominator exceeding the bound; otherwise leave it as is) and to compute the nearest approximations. But of course a better option is to look at all denominators up to that bound. But a naive check of this kind is not efficient. If we generate the Farey series, then things do improve but generating the Farey series itself takes time(even if done only once since the bound is fixed once the user selects it) Is there a fast algorithm of this kind without generating the full Farey series? Specifically an efficient algorithm to find for a given rational r, and natural N, the unique rationals r1, r2 s.t r1<=r<=r2, r1 and r2 having denominators <=N

Edit: Okay, the linked post gives me a clear idea of how to solve the problem. Thanks to Bill Dubuque. I will set down the solution here for completeness. The following is a corrected version of the solution I posted on 27-03-2025(I will indicate the problem with this earlier solution at the end) We only consider the case where r in reduced form has denominator exceeding N. Also since the integral part of r is irrelevant we can assume w.l.o.g. that 0<r<1. Let r = a/b in reduced form. Let ba = b//a(the integer part of b/a) Then if ba >= N, r<1/N so that 0 and 1/N are the desired approximations. Else set r1=1/(ba+1) r2=1/ba(Since r<1, b>a and so b//a>0) Notice that r2-r1 = 1/ba(ba+1) which has numerator 1 in reduced form. This is the crucial property(call it property P) Then find the Farey mediant r3 of r1 and r2. If it has a denominator <=N, then we continue. Otherwise r1 and r2 are themselves the desired upper and lower bounds. If r3 has a denominator <=N, then either r < r3 or r > r3. W.l.o.g. let r < r3. Once again we find the mediant r4 of r1 and r3. If this has a denominator >N, we stop and r1 and r3 are the desired solutions. Else we continue.

Let us do an example. Find the best approximations to 23/17 with N=10 Well 23/17 = 1 + 6/17 and the fractional part is 6/17 17//6 = 2 so 1/3<6/17<1/2 Taking the mediant 2/5 we get 1/3 < 6/17 < 2/5 Taking the mediant 3/8 we get 1/3 < 6/17 < 3/8 Taking the mediant we get 4/11 which is in reduced form and the denominator exceeds 10. So we are done and 1/3 and 3/8 are the desired best estimates to 6/17 so that 1+1/3(=4/3) and 1+3/8(=11/8) are the best estimates to 23/17

In the solution of 27-03-2025, r1 and r2 were taken as k/N and (k+1)/N s.t r1<r<r2. The problem is that in general k/N and (k+1)/N do not have property P. The theorem we use is that if x and y, x<y are fractions in Farey(N) with property P and if the mediant of x and y does not lie in Farey(N)(i.e its denominator in reduced form > N) then x and y are neighboring elements of Farey(N) i.e there is no z in Farey(N) s.t. x<z<y. This is false if we do not assume property P. For example for N=15, 4/15 and 5/15(=1/3) have mediant 5/18 which in not in Farey(15) But 2/7 is in Farey(15) and 4/15<2/7<1/3

Apart from the above theorem, the only other result we use in deriving the algorithm is that if x and y, x<y have property P, and if z, x<z<y is their mediant, that x, z and z, y also have property P

0 Answers0