+ADw-p+AD4-In this article, we+IBk-ll see different ways to find LCM in Python with program examples.+ADw-/p+AD4
+ADw-p+AD4-Basically LCM is a smallest number that is divisible by both numbers (or all). Let us see how we can find lcm

**of** two numbers in

**python**.+ADw-/p+AD4
+ADw-p+AD4APA-span style+AD0AIg-text-decoration: underline+ADsAIgA+ADw-strong+AD4-1. Using Loop+ADw-/strong+AD4APA-/span+AD4APA-/p+AD4
+ADw-p+AD4-First we find the larger number of two given numbers. Starting from it and will try to find the first number that is divisible by both, which is LCM.+ADw-/p+AD4APA-pre class+AD0AIg-crayon-plain-tag+ACIAPg-x+AD0-12
y+AD0-20
if x +ACY-gt+ADs y:
greater +AD0 x
else:
greater +AD0 y
while(True):
if((greater +ACU x +AD0APQ 0) and (greater +ACU y +AD0APQ 0)):
lcm +AD0 greater
break
greater +AD0 greater +ACs 1

**print** (+ACI-Least common multiple +AD0 +ACI, lcm)+ADw-/pre+AD4APA-p+AD4APA-strong+AD4-Output:+ADw-/strong+AD4APA-/p+AD4
+ADw-p+AD4APA-em+AD4-Least common multiple +AD0AoA 60+ADw-/em+AD4APA-/p+AD4
+ADw-p+AD4-In above program, first we+IBk-re finding greater number, then start a loop. Inside the loop we+IBk-ll find a number that can be divisible by both of given numbers n1 and n2. When we will get a that number we+IBk-ll store it into a new variable called+ADw-strong+AD4 lcm. +ADw-/strong+AD4-If didn+IBk-t get then we+IBk-ll increase greater by 1. As we know the number will be greater than both of the numbers that why we+IBk-re start+AKA checking lcm from greater number.+ADw-/p+AD4
+ADw-p+AD4APA-strong+AD4APA-span style+AD0AIg-text-decoration: underline+ADsAIgA+-2. Using GCD+ADw-/span+AD4APA-/strong+AD4APA-/p+AD4
+ADw-p+AD4-If you+IBk-ve the basic knowledge of mathematics, then you would know that we can find LCM very easily using GCD.+ADw-/p+AD4
+ADw-p+AD4APA-strong+AD4-Also Read:+AKAAPA-a href+AD0AIg-https://www.thecrazyprogrammer.com/2018/05/python-gcd.html+ACI rel+AD0AIg-noopener+ACIAPg-Python GCD +IBM 4 Ways to Find GCD or HCF+ADw-/a+AD4APA-/strong+AD4APA-/p+AD4
+ADw-p+AD4-Here is the formula:+ADw-/p+AD4
+ADw-p+AD4APA-strong+AD4-number 1 +ACo number 2 +AD0AoA LCM +ACo GCD+ADw-/strong+AD4APA-/p+AD4
+ADw-p+AD4-So,+ADw-/p+AD4
+ADw-p+AD4APA-strong+AD4-LCM+AKA +AD0AoACg (number 1 +ACo number 2)/GCD of number1 and 2+ADw-/strong+AD4APA-/p+AD4
+ADw-p+AD4-Let+IBk-s implement this formula into program.+ADw-/p+AD4APA-pre class+AD0AIg-crayon-plain-tag+ACIAPg-import math
def get+AF8-lcm(n1,n2):
+ACM-find gcd
gcd +AD0 math.gcd(n1,n2)
+ACM-formula
result +AD0 (n1+ACo-n2)/gcd
return result
n1 +AD0 12
n2 +AD0 20
lcm +AD0 get+AF8-lcm(n1,n2)
print(+ACI-least common multiple +AD0 +ACI, lcm)+ADw-/pre+AD4APA-p+AD4APA-strong+AD4-Output+ADw-/strong+AD4APA-/p+AD4
+ADw-p+AD4APA-em+AD4-least common multiple +AD0 60.0+ADw-/em+AD4APA-/p+AD4
+ADw-p+AD4-So in above program we+IBk-ve have function that receives two arguments and then inside it, first we+IBk-ll find GCD and after that we+IBk-re applying given formula to find out LCM with the help of GCD and return it.+ADw-/p+AD4
+ADw-p+AD4-So these were two easiest ways to get the LCM of two given numbers. But what if we have more than two numbers. So here is the program for it.+ADw-/p+AD4
+ADw-p+AD4APA-strong+AD4-How to find LCM of more than two numbers?+ADw-/strong+AD4APA-/p+AD4APA-pre class+AD0AIg-crayon-plain-tag+ACIAPg-from math import gcd
list1 +AD0 +AFs-12,48,8,60+AF0
lcm +AD0 list1+AFs-0+AF0
for i in list1+AFs-1:+AF0:
lcm +AD0 int(lcm+ACo-i/gcd(lcm, i))
print(+ACI-least common multiple +AD0 +ACI, lcm)+ADw-/pre+AD4APA-p+AD4APA-strong+AD4-Output:+ADw-/strong+AD4APA-/p+AD4
+ADw-p+AD4APA-em+AD4-least common multiple +AD0AoA 240+ADw-/em+AD4APA-/p+AD4
+ADw-p+AD4-So in above program, we have an list of numbers and then we will store first item of the list in variable +ADw-strong+AD4-lcm. +ADw-/strong+AD4-Then we will loop through all the elements present in the list1. Inside the loop we+IBk-ll multiply lcm with+AKAAPA-strong+AD4-i/GCD of lcm and i.+AKAAPA-/strong+AD4-So after breaking the loop we+IBk-ll get our LCM of all numbers in variable lcm.+ADw-/p+AD4
+ADw-p+AD4-If you+IBk-ve any problem or suggestion related to python lcm programs then please let us know in comment box.+ADw-/p+AD4
+ADw-p+AD4-The post +ADw-a rel+AD0AIg-nofollow+ACI href+AD0AIg-https://www.thecrazyprogrammer.com/2018/05/python-lcm-2-ways-to-find-lcm.html+ACIAPg-Python LCM +ACYAIw-8211+ADs 2 Ways to find LCM+ADw-/a+AD4 appeared first on +ADw-a rel+AD0AIg-nofollow+ACI href+AD0AIg-https://www.thecrazyprogrammer.com+ACIAPg-The Crazy Programmer+ADw-/a+AD4.+ADw-/p+AD4