From adffec35f6cd0311f81c46250a0d65387a276b06 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 12 Dec 2013 17:36:50 +0100 Subject: [PATCH] utils/misc: add gcd_multiple function to compute GCD or any number of integers --- migen/util/misc.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/migen/util/misc.py b/migen/util/misc.py index 6846b4a5..799c58e8 100644 --- a/migen/util/misc.py +++ b/migen/util/misc.py @@ -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:])) -- 2.30.2