Decimals -> Fraction strings, my solution

Peter Schneider-Kamp petersc at stud.ntnu.no
Wed May 17 14:17:28 EDT 2000


Dave Hansen wrote:
> 
> A very nice implementation of Euclid's algorithm.  I would suggest a
> couple small changes to make it more robust.  Shouldn't hurt the
> efficiency significantly...
> 
> >
> >def gcd(a, b):
>      a = abs(a)
>      b = abs(b)

Probably a good idea given Python's implementation of modulo.

>      if b > a:
>          a, b = b, a

This is IMHO absolutely unneccessary and just bloats the code.
Why do you want to check when in the case that b is greater
than a the first step just reverses the arguments? So in case
b is greater than a you trade in a division against a
comparison and more code. In the case that a is already
greater than b you gain nothing and pay a comparision and
more code.

Ha det bra
Peter
--
Peter Schneider-Kamp          ++47-7388-7331
Herman Krags veg 51-11        mailto:peter at schneider-kamp.de
N-7050 Trondheim              http://schneider-kamp.de




More information about the Python-list mailing list