Decimals -> Fraction strings, my solution

Tim Peters tim_one at email.msn.com
Wed May 17 02:03:06 EDT 2000


[François Pinard, with a continued fraction algorithm]
> ...
> I do not think GCD is necessary, but I did not prove this to myself.

Take heart, it's not.  For adjacent pairs

   a  c
   -, -
   b  d

in a cf expansion, a*d - b*c alternates between +1 and -1, which is an easy
inductive proof (provided you start with two for which this is true, as is
the case for the usual starting pair 0/1 and 1/0).  So anything dividing
both a and b (or c and d) divides a*d - b*c = 1 too, so they're relatively
prime.

> ...
>             residual = 1.0 / residual

Note that this step can introduce a small error on each iteration, due to
the vagaries of floating-point arithmetic.  To do this with complete
accuracy requires sticking to rational arithmetic throughout.

bot-good-enough-as-is-for-all-sane-purposes!-ly y'rs  - tim






More information about the Python-list mailing list