[Tutor] Re: LCM problem
Thu, 27 Jan 2000 12:37:32 -0800 (PST)
> Hey python community,
> I got it to count 2 numbers multiples.... now how do i get it to =
> find the least multiple of each number.
Are you familiar with the nice definition of LCM that involves the GCD?
If you can find the GCD of a list of numbers, then you divide each number
by the GCD, and then multiply the result list together.
Let's pretend that we have a gcd(x,y) function. Then we could do the
following on a list of numbers L:
common_gcd = reduce(gcd, L) # this will find the gcd among all in L
L2 = map( lambda x: x/common_gcd, L )
After that, just multiply all the numbers in L2 together, and you should
have the LCM of those numbers.