From 0533e3caaf5bc93d163768a89cb88c41ac068428 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 13 Jul 2015 16:28:16 +1000 Subject: [PATCH] maths: add gcd implementation --- maths.hpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/maths.hpp b/maths.hpp index 94991d72..d896c90e 100644 --- a/maths.hpp +++ b/maths.hpp @@ -101,6 +101,21 @@ unsigned digits [[gnu::pure]] (const T& value); +//----------------------------------------------------------------------------- +// factorisation +template +constexpr T +gcd (T a, T b) +{ + if (a == b) return a; + + if (a > b) return gcd (a - b, b); + if (b > a) return gcd (a, b - a); + + unreachable (); +} + + //----------------------------------------------------------------------------- constexpr int sign (int); constexpr float sign (float);