Computer >> Computer tutorials >  >> Programming >> Python

How to Find LCM using Python?


 LCM (Least common multiple) of two (or more) numbers is a number which is the smallest number that is divisible by both (or all).

First we find the larger number of two given numbers. Starting from it we try and find the first number that is divisible by both, which is LCM

Example

x=12
y=20
if x > y:  
   greater = x  
else:  
   greater = y  
while(True):  
   if((greater % x == 0) and (greater % y == 0)):  
        lcm = greater  
        break  
    greater += 1

print ("LCM of {} and {}={}".format(x,y,lcm))

Output

The result is −

LCM of 12 and 20=60