diff --git a/NumberTheory/Factors.cpp b/NumberTheory/Factors.cpp deleted file mode 100644 index d383efc79..000000000 --- a/NumberTheory/Factors.cpp +++ /dev/null @@ -1,36 +0,0 @@ -// -// Factors.cpp -// Clock Signal -// -// Created by Thomas Harte on 29/07/2016. -// Copyright © 2016 Thomas Harte. All rights reserved. -// - -#include "Factors.hpp" - -unsigned int NumberTheory::greatest_common_divisor(unsigned int a, unsigned int b) -{ - if(a < b) - { - unsigned int swap = b; - b = a; - a = swap; - } - - while(1) { - if(!a) return b; - if(!b) return a; - - unsigned int remainder = a%b; - a = b; - b = remainder; - } -} - -unsigned int NumberTheory::least_common_multiple(unsigned int a, unsigned int b) -{ - if(a == b) return a; - - unsigned int gcd = greatest_common_divisor(a, b); - return (a / gcd) * (b / gcd) * gcd; -} diff --git a/NumberTheory/Factors.hpp b/NumberTheory/Factors.hpp index d2c280a11..03ffc8593 100644 --- a/NumberTheory/Factors.hpp +++ b/NumberTheory/Factors.hpp @@ -13,13 +13,33 @@ namespace NumberTheory { /*! @returns The greatest common divisor of @c a and @c b as computed by Euclid's algorithm. */ - unsigned int greatest_common_divisor(unsigned int a, unsigned int b); + template T greatest_common_divisor(T a, T b) { + if(a < b) { + T swap = b; + b = a; + a = swap; + } + + while(1) { + if(!a) return b; + if(!b) return a; + + T remainder = a%b; + a = b; + b = remainder; + } + } /*! @returns The least common multiple of @c a and @c b computed indirectly via Euclid's greatest common divisor algorithm. */ - unsigned int least_common_multiple(unsigned int a, unsigned int b); + template T least_common_multiple(T a, T b) { + if(a == b) return a; + + T gcd = greatest_common_divisor(a, b); + return (a / gcd) * (b / gcd) * gcd; + } } #endif /* Factors_hpp */