+2 votes
in Programming Languages by (14.6k points)
How to compute the greatest common divisor of numbers in Python?

E.g.

gcd(5,10) = 5

1 Answer

0 votes
by (25.6k points)

You can use the gcd() function of Numpy to compute the greatest common divisor of numbers.

If you have only 2 numbers:

>>> import numpy as np
>>> np.gcd(5,10)
5
>>> np.gcd(15,16)
1

If you have more than 2 numbers:

>>> np.gcd.reduce([8,14,16])
2
>>> np.gcd.reduce([8,40,45])
1

...