Calculating the largest common denominator in python

If you have a list of integers in python, let's say L = [4,8,12,24] , how can you calculate their greatest common denominator / divisor (in this case 4)?

+4
source share
2 answers

One way to do this:

 import fractions def gcd(L): return reduce(fractions.gcd, L) print gcd([4,8,12,24]) 
+20
source

good, this will reduce the time complexity for a huge list (len> 10 ^ 9)

0
source

All Articles