The least common Multiple Python is the code that will calculate the least common multiple (LCM) of two numbers. The least common multiple is a number that will be perfectly divisible by both numbers. For example, the LCM of 6 and 4 is 12. 12 is perfectly divisible by both 4 and 6 and is the smallest multiple to satisfy the condition.
Program to Compute LCM
Commonly, to find the LCM of any two numbers, first, their prime factors are found. After this, the union of all the prime factors that are common in the two numbers are found. The product of this union gives the LCM or least common multiple of both the numbers.
For example, the prime factors of 6 are 2 and 3. For 4, the prime factors are 2,2. The common factor here is 2. The union will be formed by taking the common factor 2 once, along with 2 and 3. The multiplication of 2, 2, and 3 is 12. This is the LCM of the 4, 6.
The code provided below for the least common multiple Python uses a function that will compute the LCM of the two numbers. The parameters passed in the function are the numbers that will be input by the user. The function will return the calculated LCM of the two numbers which will then be printed on the output screen.
def calc_lcm (x, y) if x > y: greater = x else: greater = y while (True): if ((greater % x == 0) and (greater % y == 0 )): lcm = greater break greater += 1 return lcm num1 = int (input (“Enter the first number: “)) num2 = int (input (“Enter the second number: “)) print (“The LCM of the provided two numbers is”, calc_lcm (num1, num2))
The program above first checks which of the two numbers provided by the user is greater. If the greater number is completely divisible by both the numbers, then the greater number is the LCM itself.
If not, then the loop will run by increasing the greater number by 1 with each iteration and checking if it is divisible by both the numbers. Once the condition of the WHILE loop is true, then the function will print the greater number as the lcm.
The program goes through each natural number one by one. This can take a long time if the numbers that are input are quite large or prime numbers themselves.
Program to Compute LCM Using GCD
Another method that can be used to calculate the least common multiple Python is with the help of GCD or the Greatest Common Divisor. The greatest common divisor is the greatest number that can perfectly divide both the entered numbers. The formula through which LCM can be calculated with the help of GCD is as follows.
Product of two numbers = LCM * GCD
The program thus created will be faster to execute and will provide results for even large prime numbers. The code is provided below.
def calc_gcd (x, y) while (y): x, y = y, x % y return x def calc_lcm (x, y) lcm = (x*y) // calc_gcd (x, y) return lcm num1 = int (input (“Enter the first number: “) num2 = int (input (“Enter the second number: “) print (“The LCM of the given numbers is” compute_lcm (num1, num2))
The output obtained by both the programs will be the same. However, with the second program, you can save time by decreasing the compilation and execution time of the program.
The program includes two functions. The first calculates the GCD of the given two numbers by finding the factors after dividing the first number by the second number.
Once the GCD is calculated, the function to calculate the LCM will put the values in the formula and calculate the LCM.
Frequently Asked Questions
How do you find the least common multiple in Python?
The least common multiple Python can be calculated with the help of prime factorisation of the numbers or with the help of GCD. Both the methods provide similar results. However, the latter is preferred for its speed.
How do I find the least common multiple?
To find the least common multiple of any two numbers, first, find the prime factors of the two numbers. Then, form a union of these prime factors that include the common factors once. Multiply the numbers of the union and you will get the LCM of the numbers.
How do you find the LCD in Python?
The least common divisor can be found by calculating the GCD of the given two numbers, once found, the LCD can be found by dividing the GCD by the product of the two numbers.
How do you find the HCF and LCM in Python?
With factors of the two numbers, the HCF and LCM can be calculated.