2016-07-29 09:19:01 +00:00
|
|
|
//
|
|
|
|
// Factors.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 29/07/2016.
|
|
|
|
// Copyright © 2016 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef Factors_hpp
|
|
|
|
#define Factors_hpp
|
|
|
|
|
2017-07-21 22:18:45 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2016-07-29 09:19:01 +00:00
|
|
|
namespace NumberTheory {
|
2016-08-01 10:04:55 +00:00
|
|
|
/*!
|
|
|
|
@returns The greatest common divisor of @c a and @c b as computed by Euclid's algorithm.
|
|
|
|
*/
|
2016-09-18 14:23:15 +00:00
|
|
|
template<class T> T greatest_common_divisor(T a, T b) {
|
|
|
|
if(a < b) {
|
2017-07-21 22:18:45 +00:00
|
|
|
std::swap(a, b);
|
2016-09-18 14:23:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
if(!a) return b;
|
|
|
|
if(!b) return a;
|
|
|
|
|
|
|
|
T remainder = a%b;
|
|
|
|
a = b;
|
|
|
|
b = remainder;
|
|
|
|
}
|
|
|
|
}
|
2016-08-01 10:04:55 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns The least common multiple of @c a and @c b computed indirectly via Euclid's greatest
|
|
|
|
common divisor algorithm.
|
|
|
|
*/
|
2016-09-18 14:23:15 +00:00
|
|
|
template<class T> T least_common_multiple(T a, T b) {
|
|
|
|
if(a == b) return a;
|
|
|
|
|
|
|
|
T gcd = greatest_common_divisor<T>(a, b);
|
|
|
|
return (a / gcd) * (b / gcd) * gcd;
|
|
|
|
}
|
2016-07-29 09:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* Factors_hpp */
|