Lcm of 3 numbers in python

To calculate the GCD of more than two numbers you can do it as follows:

For example: GCD of a, b, c, d

  1. gcd[a, b] = x
  2. gcd[x, c] = y
  3. gcd[y, d] = z

And to calculate the LCM of more than two numbers you can do it as follows:

For example: LCM of a, b, c, d

  1. a * b // gcd[a, b] = x
  2. x * c // gcd[x, c] = y
  3. y * d // gcd[y, d] = z

*Double-slash operator [//] for "floor" division [rounds down to nearest whole number].

GCD in Python 3:

So first we could do it with the following simple while iteration and using gcd[] function from math module as follows:

from math import gcd

def gcd_n[*args]:
    i = 1
    x = args[0]

    while i < len[args]:
        x = gcd[x, args[i]]
        i += 1
    return x

It can also be made a little simpler with a lambda function and using reduce[] function from functools module as follows:

from math import gcd
from functools import reduce

def gcd_n[*args]:
    f = lambda a,b:gcd[a,b]
    return reduce[lambda x,y:f[x,y],args]

But when you think it can't be made easier ... YES, it can be made even easier:

from math import gcd
from functools import reduce

def gcd_n[*args]:
    return reduce[gcd, args]

Either way the return is the same. With this simple functions you can calculate the GCD of all the numbers you want, no matter if they are 2, 3, 4 or N numbers.

>>> gcd_n[3355, 985]
5
>>> gcd_n[3465, 6615, 7875]
315
>>> gcd_n[6930, 13230, 15760]
10
>>> gcd_n[1750, 1960, 3080]
70
>>> gcd_n[85, 96, 100, 225]
1

LCM in Python 3:

Following the same philosophy of the simplest example above, to calculate the LCM of two or more numbers, these two functions would suffice:

from math import gcd
from functools import reduce

def lcm[a, b]:
    return a * b // gcd[a, b]

def lcm_n[*args]:
    return reduce[lcm, args]

Here you have a sample test series:

>>> lcm_n[77, 9]
693
>>> lcm_n[5, 10, 15]
30
>>> lcm_n[62, 89, 13]
71734
>>> lcm_n[21, 4, 17, 2]
1428

I hope it helps, it works well for me.

Home » Python » Python programs

Here, we will learn how to find the LCM [lowest common multiple] of the arrays elements in the Python programming language?
Submitted by Bipin Kumar, on November 19, 2019

LCM is the lowest multiple of two or more numbers. Multiples of a number are those numbers which when divided by the number leave no remainder. When we are talking about the multiple, we consider only the positive number. For example, the LCM of 12 and 48 is 48 because 48 is 4th multiple of 12 and 48 is 1st multiple of 48. Here, an array of positive elements will be provided by the user and we have to find the LCM of the elements of the array by using the Python. To find LCM of the elements of the array, use the following algo,

Algorithm to find the LCM of array elements

  • We need to import the math module to find the GCD of two numbers using math.gcd[] function.
  • At first, find the LCM of initial two numbers using: LCM[a,b] = a*b/GCD[a,b]. And, then find the LCM of three numbers with the help of LCM of first two numbers using LCM[ab,c] = lcm[lcm[a1, a2], a3]. The same concept we have implemented.

Now, we will write the Python program in a simple way by implementing the above algorithm.

Program:

# importing the module
import math

# function to calculate LCM
def LCMofArray[a]:
  lcm = a[0]
  for i in range[1,len[a]]:
    lcm = lcm*a[i]//math.gcd[lcm, a[i]]
  return lcm


# array of integers
arr1 = [1,2,3]
arr2 = [2,3,4]
arr3 = [3,4,5]
arr4 = [2,4,6,8]
arr5 = [8,4,12,40,26,28]

print["LCM of arr1 elements:", LCMofArray[arr1]]
print["LCM of arr2 elements:", LCMofArray[arr2]]
print["LCM of arr3 elements:", LCMofArray[arr3]]
print["LCM of arr4 elements:", LCMofArray[arr4]]
print["LCM of arr5 elements:", LCMofArray[arr5]]

Output

LCM of arr1 elements: 6
LCM of arr2 elements: 12
LCM of arr3 elements: 60
LCM of arr4 elements: 24
LCM of arr5 elements: 10920

Python Array Programs »


How do you find the LCM of 3 numbers in Python?

num1 = int[input["Enter first number: "]] num2 = int[input["Enter second number: "]] # printing the result for the users. print["The L.C.M. of", num1,"and", num2,"is", calculate_lcm[num1, num2]]

Is there a LCM function in Python?

The math module in Python contains a number of mathematical operations. Amongst some of the most important functions in this module is the lcm[] function which returns the least common multiple of the specified integer arguments. The lcm function was newly introduced in the Python version 3.9. 0.

How do you find the least common multiple of a list in Python?

Algorithm to find the LCM of array elements gcd[] function. At first, find the LCM of initial two numbers using: LCM[a,b] = a*b/GCD[a,b]. And, then find the LCM of three numbers with the help of LCM of first two numbers using LCM[ab,c] = lcm[lcm[a1, a2], a3].

Chủ Đề