[Tutor] A better way for greatest common divisor
David Hutto
smokefloat at gmail.com
Fri Jul 30 04:57:19 CEST 2010
On Thu, Jul 29, 2010 at 10:16 PM, James Mills
<prologic at shortcircuit.net.au> wrote:
> 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
That was pretty short, and sweet.
>
> 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"
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list