common_factor_rt.hpp 578 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #ifndef BOOST_MATH_COMMON_FACTOR_RT_HPP
  2. #define BOOST_MATH_COMMON_FACTOR_RT_HPP
  3. namespace boost {
  4. namespace math {
  5. // TODO: use binary GCD for unsigned integers ....
  6. template < typename IntegerType >
  7. IntegerType gcd( IntegerType a, IntegerType b )
  8. {
  9. const IntegerType zero = (IntegerType)0;
  10. while ( true )
  11. {
  12. if ( a == zero )
  13. return b;
  14. b %= a;
  15. if ( b == zero )
  16. return a;
  17. a %= b;
  18. }
  19. }
  20. template < typename IntegerType >
  21. IntegerType lcm( IntegerType a, IntegerType b )
  22. {
  23. const IntegerType t = gcd (a,b);
  24. if (!t)return t;
  25. return a / t * b;
  26. }
  27. }}
  28. #endif