utils/misc: add gcd_multiple function to compute GCD or any number of integers
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Thu, 12 Dec 2013 16:36:50 +0000 (17:36 +0100)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Thu, 12 Dec 2013 16:36:50 +0000 (17:36 +0100)
migen/util/misc.py

index 6846b4a5bab47a1de2be33c4f653386bdee53d3d..799c58e8fe3a9bbccd7a3234432fb08f3c48240f 100644 (file)
@@ -1,3 +1,4 @@
+from fractions import gcd
 import collections
 
 def flat_iteration(l):
@@ -26,3 +27,11 @@ def autotype(s):
        except ValueError:
                pass
        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:]))