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

E.g.

gcd(5,10) = 5

1 Answer

+1 vote
by (349k points)
selected by
 
Best answer

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


...