If you have a list of integers in python, say L = [4,8,12,24]
, how can you compute their greatest common denominator/divisor (4 in this case)?
Asked
Active
Viewed 5,953 times
1 Answers
26
One way to do it is:
import fractions
def gcd(L):
return reduce(fractions.gcd, L)
print gcd([4,8,12,24])

Tim Pietzcker
- 328,213
- 58
- 503
- 561

D R
- 21,936
- 38
- 112
- 149
-
1Great example for getting short code with python. I was searching exactly for *that* kind of solution. – Wolf Apr 20 '15 at 13:13
-
This is correct only for Python 2.X. For Python 3.X see answer https://stackoverflow.com/a/61779475/4769067 – Ilya Panin Nov 24 '20 at 19:51