[Tutor] A better way for greatest common divisor

James Mills prologic at shortcircuit.net.au
Fri Jul 30 04:16:46 CEST 2010


On Fri, Jul 30, 2010 at 12:10 PM, James Mills
<prologic at shortcircuit.net.au> wrote:
> def gcd(a, b):
>    while b != 0:
>        (a, b) = (b, a%b)
>    return a

Here's another solution that uses a generator called factors to
generate a list of factors for any given value. The gcd function
then uses sets and intersection and the max function to find
the greatest common factor/divisor

http://codepad.org/VJIRyvI8

cheers
James

-- 
-- James Mills
--
-- "Problems are solved by method"


More information about the Tutor mailing list