mirror of
https://github.com/boostorg/math.git
synced 2026-01-28 07:22:12 +00:00
263 lines
9.9 KiB
HTML
263 lines
9.9 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
|
|
<html>
|
|
<head>
|
|
<title>Boost GCD & LCM Library</title>
|
|
</head>
|
|
|
|
<body bgcolor="white" text="black" link="blue" alink="red" vlink="purple">
|
|
<h1>
|
|
<img src="../../../boost.png" alt="boost.png (6897 bytes)"
|
|
align="left" width="277" height="86">Greatest Common Divisor<br>
|
|
Least
|
|
Common Multiple<br>
|
|
</h1>
|
|
|
|
<p>The class and function templates in <cite><a
|
|
href="../../../boost/math/common_factor.hpp"><boost/math/common_factor.hpp></a>
|
|
</cite> provide run-time and compile-time evaluation of the greatest
|
|
common divisor (GCD) or least common multiple (LCM) of two integers.
|
|
These facilities are useful for many numeric-oriented generic
|
|
programming problems.</p>
|
|
|
|
<h2><a name="contents">Contents</a></h2>
|
|
|
|
<ul>
|
|
<li><a href="#contents">Contents</a></li>
|
|
<li>Header <cite><a href="#cf_hpp"><boost/math/common_factor.hpp></a></cite>
|
|
<ul>
|
|
<li><a href="#synopsis">Synopsis</a></li>
|
|
</ul></li>
|
|
<li>Header <cite><a href="#cfrt_hpp"><boost/math/common_factor_rt.hpp></a></cite>
|
|
<ul>
|
|
<li><a href="#gcd_obj">GCD Function Object</a></li>
|
|
<li><a href="#lcm_obj">LCM Function Object</a></li>
|
|
<li><a href="#run_gcd_lcm">Run-time GCD & LCM Determination</a></li>
|
|
</ul></li>
|
|
<li>Header <cite><a href="#cfct_hpp"><boost/math/common_factor_ct.hpp></a></cite></li>
|
|
<li><a href="#example">Example</a></li>
|
|
<li><a href="#demo">Demonstration Program</a></li>
|
|
<li><a href="#rationale">Rationale</a></li>
|
|
<li><a href="#history">History</a></li>
|
|
<li><a href="#credits">Credits</a></li>
|
|
</ul>
|
|
|
|
<h2>Header <cite><a name="cf_hpp" href="../../../boost/math/common_factor.hpp"><boost/math/common_factor.hpp></a></cite></h2>
|
|
|
|
<p>This header simply includes the headers <cite><a
|
|
href="../../../boost/math/common_factor_ct.hpp"><boost/math/common_factor_ct.hpp></a></cite>
|
|
and <cite><a
|
|
href="../../../boost/math/common_factor_rt.hpp"><boost/math/common_factor_rt.hpp></a></cite>.
|
|
It used to contain the code, but the compile-time and run-time
|
|
facilities were moved to separate headers (since they were independent),
|
|
and this header maintains compatibility.</p>
|
|
|
|
<h3><a name="synopsis">Synopsis</a></h3>
|
|
|
|
<blockquote><pre>namespace boost
|
|
{
|
|
namespace math
|
|
{
|
|
|
|
template < typename IntegerType >
|
|
class gcd_evaluator;
|
|
template < typename IntegerType >
|
|
class lcm_evaluator;
|
|
|
|
template < typename IntegerType >
|
|
IntegerType gcd( IntegerType const &a, IntegerType const &b );
|
|
template < typename IntegerType >
|
|
IntegerType lcm( IntegerType const &a, IntegerType const &b );
|
|
|
|
template < unsigned long Value1, unsigned long Value2 >
|
|
struct static_gcd;
|
|
template < unsigned long Value1, unsigned long Value2 >
|
|
struct static_lcm;
|
|
|
|
}
|
|
}
|
|
</pre></blockquote>
|
|
|
|
<h2>Header <cite><a name="cfrt_hpp" href="../../../boost/math/common_factor_rt.hpp"><boost/math/common_factor_rt.hpp></a></cite></h2>
|
|
|
|
<h3><a name="gcd_obj">GCD Function Object</a></h3>
|
|
|
|
<blockquote><pre>template < typename IntegerType >
|
|
class boost::math::gcd_evaluator
|
|
{
|
|
public:
|
|
// Types
|
|
typedef IntegerType result_type;
|
|
typedef IntegerType first_argument_type;
|
|
typedef IntegerType second_argument_type;
|
|
|
|
// Function object interface
|
|
result_type operator ()( first_argument_type const &a,
|
|
second_argument_type const &b ) const;
|
|
};
|
|
</pre></blockquote>
|
|
|
|
<p>The <code>boost::math::gcd_evaluator</code> class template defines a
|
|
function object class to return the greatest common divisor of two
|
|
integers. The template is parameterized by a single type, called
|
|
<code>IntegerType</code> here. This type should be a numeric type that
|
|
represents integers. The result of the function object is always
|
|
nonnegative, even if either of the operator arguments is negative.</p>
|
|
|
|
<p>This function object class template is used in the corresponding
|
|
version of the <a href="#run_gcd_lcm">GCD function template</a>. If a
|
|
numeric type wants to customize evaluations of its greatest common
|
|
divisors, then the type should specialize on the
|
|
<code>gcd_evaluator</code> class template.</p>
|
|
|
|
<h3><a name="lcm_obj">LCM Function Object</a></h3>
|
|
|
|
<blockquote><pre>template < typename IntegerType >
|
|
class boost::math::lcm_evaluator
|
|
{
|
|
public:
|
|
// Types
|
|
typedef IntegerType result_type;
|
|
typedef IntegerType first_argument_type;
|
|
typedef IntegerType second_argument_type;
|
|
|
|
// Function object interface
|
|
result_type operator ()( first_argument_type const &a,
|
|
second_argument_type const &b ) const;
|
|
};
|
|
</pre></blockquote>
|
|
|
|
<p>The <code>boost::math::lcm_evaluator</code> class template defines a
|
|
function object class to return the least common multiple of two
|
|
integers. The template is parameterized by a single type, called
|
|
<code>IntegerType</code> here. This type should be a numeric type that
|
|
represents integers. The result of the function object is always
|
|
nonnegative, even if either of the operator arguments is negative. If
|
|
the least common multiple is beyond the range of the integer type, the
|
|
results are undefined.</p>
|
|
|
|
<p>This function object class template is used in the corresponding
|
|
version of the <a href="#run_gcd_lcm">LCM function template</a>. If a
|
|
numeric type wants to customize evaluations of its least common
|
|
multiples, then the type should specialize on the
|
|
<code>lcm_evaluator</code> class template.</p>
|
|
|
|
<h3><a name="run_gcd_lcm">Run-time GCD & LCM Determination</a></h3>
|
|
|
|
<blockquote><pre>template < typename IntegerType >
|
|
IntegerType boost::math::gcd( IntegerType const &a, IntegerType const &b );
|
|
|
|
template < typename IntegerType >
|
|
IntegerType boost::math::lcm( IntegerType const &a, IntegerType const &b );
|
|
</pre></blockquote>
|
|
|
|
<p>The <code>boost::math::gcd</code> function template returns the
|
|
greatest common (nonnegative) divisor of the two integers passed to it.
|
|
The <code>boost::math::lcm</code> function template returns the least
|
|
common (nonnegative) multiple of the two integers passed to it. The
|
|
function templates are parameterized on the function arguments'
|
|
<var>IntegerType</var>, which is also the return type. Internally,
|
|
these function templates use an object of the corresponding version of
|
|
the <code><a href="#gcd_obj">gcd_evaluator</a></code> and <code><a
|
|
href="#lcm_obj">lcm_evaluator</a></code> class templates,
|
|
respectively.</p>
|
|
|
|
<h2>Header <cite><a name="cfct_hpp" href="../../../boost/math/common_factor_ct.hpp"><boost/math/common_factor_ct.hpp></a></cite></h2>
|
|
|
|
<blockquote><pre>template < unsigned long Value1, unsigned long Value2 >
|
|
struct boost::math::static_gcd
|
|
{
|
|
static unsigned long const value = <em>implementation_defined</em>;
|
|
};
|
|
|
|
template < unsigned long Value1, unsigned long Value2 >
|
|
struct boost::math::static_lcm
|
|
{
|
|
static unsigned long const value = <em>implementation_defined</em>;
|
|
};
|
|
</pre></blockquote>
|
|
|
|
<p>The <code>boost::math::static_gcd</code> and
|
|
<code>boost::math::static_lcm</code> class templates take two
|
|
value-based template parameters of the <code>unsigned long</code> type
|
|
and have a single static constant data member, <code>value</code>, 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 <code>unsigned long</code>.</p>
|
|
|
|
<h2><a name="example">Example</a></h2>
|
|
|
|
<blockquote><pre>#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, " ") );
|
|
}
|
|
</pre></blockquote>
|
|
|
|
<h2><a name="demo">Demonstration Program</a></h2>
|
|
|
|
<p>The program <a
|
|
href="../test/common_factor_test.cpp">common_factor_test.cpp</a> is a
|
|
demonstration of the results from instantiating various examples of the
|
|
run-time GCD and LCM function templates and the compile-time GCD and
|
|
LCM class templates. (The run-time GCD and LCM class templates are
|
|
tested indirectly through the run-time function templates.)</p>
|
|
|
|
<h2><a name="rationale">Rationale</a></h2>
|
|
|
|
<p>The greatest common divisor and least common multiple functions are
|
|
greatly used in some numeric contexts, including some of the other Boost
|
|
libraries. Centralizing these functions to one header improves code
|
|
factoring and eases maintainence.</p>
|
|
|
|
<h2><a name="history">History</a></h2>
|
|
|
|
<dl>
|
|
<dt>2 Jul 2002
|
|
<dd>Compile-time and run-time items separated to new headers.
|
|
|
|
<dt>7 Nov 2001
|
|
<dd>Initial version
|
|
</dl>
|
|
|
|
<h2><a name="credits">Credits</a></h2>
|
|
|
|
<p>The author of the Boost compilation of GCD and LCM computations is <a
|
|
href="../../../people/daryle_walker.html">Daryle Walker</a>. The code was
|
|
prompted by existing code hiding in the implementations of <a
|
|
href="../../../people/paul_moore.htm">Paul Moore</a>'s <a
|
|
href="../../rational/index.html">rational</a> library and Steve Cleary's <a
|
|
href="../../pool/doc/index.html">pool</a> library. The code had updates by
|
|
Helmut Zeisel.</p>
|
|
|
|
<hr>
|
|
|
|
<p>Revised July 2, 2002</p>
|
|
|
|
<p>© Copyright Daryle Walker 2001-2002. Permission to copy, use,
|
|
modify, sell and distribute this document is granted provided this
|
|
copyright notice appears in all copies. This document is provided
|
|
"as is" without express or implied warranty, and with no claim
|
|
as to its suitability for any purpose.</p>
|
|
</body>
|
|
</html> |