[Tutor] LCM

Dave Angel d at davea.name
Sun Nov 18 21:27:49 CET 2012


On 11/18/2012 12:50 PM, Andrew wrote:
> On Wed, 14 Nov 2012 19:52:03 +0200, Selby Rowley Cannon
> <selbyrowleycannon at ymail.com> wrote:
>
>> Hey,
>>
>>      I've been trying to write a function to find the Lowest Common
>> Multiple of two numbers, but it isn't working and I've kinda hit a
>> dead end on the thought-  <snip>

>> I have script I wonder if it would be suitable:
>
>
> def LCM(x,y):
>     for n in range(1, 10000):
>         for n2 in range(1, 10000):
>             x_lcm = x*n
>             y_lcm = y*n2
>             if x_lcm == y_lcm:
>                 print"The LCM of",x,"and",y,"is",x_lcm
>                 return x_lcm
>             else:
>                 pass
>

That won't usually provide the LCM, it'll  just provide some common
multiple.  Occasionally, it'll happen to be right.

Rather than returning the first value that's equal, you'd have to return
the lowest value that's equal.

-- 

DaveA



More information about the Tutor mailing list