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)?
views:
102answers:
1
+5
A:
One way to do it is:
import fractions
def gcd(L):
return reduce(fractions.gcd, L)
print gcd([4,8,12,24])
celil
2010-09-04 04:53:43
+1 elegant self answer.
msw
2010-09-04 05:02:10
First line should read `import fractions`.
Etaoin
2010-09-04 05:26:06