[Tutor] Real LCM

alan.gauld@bt.com alan.gauld@bt.com
Thu, 19 Jul 2001 17:02:51 +0100


> I finally have the gcd and lcm for any length list of 
> numbers.
> ----------------------------------------------------------
> def __gcd__(a,b):
>     if b==0:
>         return a
>     return __gcd__(b,a%b)
> 
> def gcd(list):
>     list.sort()
>     g=list[0]
>     for i in range(len(list)):
>         g=__gcd__(g,list[i])
>     return g

def gcd(lst):
    lst.sort()
    return reduce(__gcd__, lst)

I think that should do the same thing?

Alan G