utils/misc: add gcd_multiple function to compute GCD or any number of integers

This commit is contained in:
Sebastien Bourdeauducq 2013-12-12 17:36:50 +01:00
parent c13fe1bc63
commit adffec35f6
1 changed files with 9 additions and 0 deletions

View File

@ -1,3 +1,4 @@
from fractions import gcd
import collections import collections
def flat_iteration(l): def flat_iteration(l):
@ -26,3 +27,11 @@ def autotype(s):
except ValueError: except ValueError:
pass pass
return s return s
def gcd_multiple(numbers):
l = len(numbers)
if l == 1:
return numbers[0]
else:
s = l//2
return gcd(gcd_multiple(numbers[:s]), gcd_multiple(numbers[s:]))