Question on if, while, for

Cimarron Taylor cimarron+google at taylors.org
Wed Feb 20 14:32:04 EST 2002


> And to spoil all the fun, here is a solution
> 
> >>> def gcd(a, b):
> ...  while a > 0:
> ...   print 'a = %d, b = %d' % (a, b)
> ...   rest = b % a
> ...   b = a
> ...   a = rest
> ...  return b
> 
> - hans kristian -

I prefer the definition found in Python22/Lib/test/test_binop.py:

def gcd(a, b):
    """Greatest common divisor using Euclid's algorithm."""
    while a:
        a, b = b%a, a
    return b

Cim



More information about the Python-list mailing list