Introduction
In mathematics, the greatest common divisor (GCD) of two or more integers, is the largest positive integer that divides each of the integers without a remainder. For example, the GCD of 8 and 12 is 4.
This tutorial is from open-source community. Access the source code
In mathematics, the greatest common divisor (GCD) of two or more integers, is the largest positive integer that divides each of the integers without a remainder. For example, the GCD of 8 and 12 is 4.
Write a function called gcd(numbers)
that takes a list of integers as an argument and returns their greatest common divisor. Your function should use functools.reduce()
and math.gcd()
over the given list.
from functools import reduce
from math import gcd as _gcd
def gcd(numbers):
return reduce(_gcd, numbers)
gcd([8, 36, 28]) ## 4
In this challenge, you have learned how to calculate the greatest common divisor of a list of numbers using functools.reduce()
and math.gcd()
.