Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.
PrevUpHomeNext

Compile time GCD and LCM determination

Header: <boost/math/common_factor_ct.hpp>

template < unsigned long Value1, unsigned long Value2 >
struct boost::math::static_gcd
{
   static unsigned long const  value = implementation_defined;
};

template < unsigned long Value1, unsigned long Value2 >
struct boost::math::static_lcm
{
   static unsigned long const  value = implementation_defined;
};

The boost::math::static_gcd and boost::math::static_lcm class templates take two value-based template parameters of the unsigned long type and have a single static constant data member, value, of that same type. The value of that member is the greatest common factor or least common multiple, respectively, of the template arguments. A compile-time error will occur if the least common multiple is beyond the range of an unsigned long.

Example

#include <boost/math/common_factor.hpp>
#include <algorithm>
#include <iterator>


int main()
{
   using std::cout;
   using std::endl;

   cout << "The GCD and LCM of 6 and 15 are "
   << boost::math::gcd(6, 15) << " and "
   << boost::math::lcm(6, 15) << ", respectively."
   << endl;

   cout << "The GCD and LCM of 8 and 9 are "
   << boost::math::static_gcd<8, 9>::value
   << " and "
   << boost::math::static_lcm<8, 9>::value
   << ", respectively." << endl;

   int  a[] = { 4, 5, 6 }, b[] = { 7, 8, 9 }, c[3];
   std::transform( a, a + 3, b, c, boost::math::gcd_evaluator<int>() );
   std::copy( c, c + 3, std::ostream_iterator<int>(cout, " ") );
}

PrevUpHomeNext