Use the math.gcd()
function
#import the math modules
import math
#The numbers whose gcd we want
nums = (20, 30, 40, 50)
#get the gcd
gcd = math.gcd(*nums)
print(gcd)
The math.gcd()
function is used to get the greatest common divisor (GCD) of two or more integers. The GCD of given numbers is the largest positive integer that divides all of the integers without a remainder.
For example the GCD of 12 and 15 is 3, because 3 is the largest number that can divide both 12 and 15 without a remainder.
math.gcd(*args)
Where, parameter args
represents the arbitrary integer arguments that can be passed to the function. The arguments should have a minimum of 2 integers.
import math
math.gcd(12, 15)
//3
math.gcd(10, 20)
//10
math.gcd(144, 228)
//12
math.gcd(-15, -10, -12)
//1
math.gcd(-4, 6, 8, 10)
//2
math.gcd(4, 12, 16, 20)
//4
math.gcd(50, 100, 150, 200, 250, 300)
//50
The arguments passed to the math.gcd()
function should be strictly integers. Otherwise a TypeError
is raised.
import math
math.gcd(3, 4.0)