math-gcd.qbk 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. [section:gcd_lcm Greatest Common Divisor and Least Common Multiple]
  2. [section Introduction]
  3. The class and function templates in <boost/math/common_factor.hpp>
  4. provide run-time and compile-time evaluation of the greatest common divisor
  5. (GCD) or least common multiple (LCM) of two integers.
  6. These facilities are useful for many numeric-oriented generic
  7. programming problems.
  8. [endsect]
  9. [section Synopsis]
  10. namespace boost
  11. {
  12. namespace integer
  13. {
  14. template < typename IntegerType >
  15. class gcd_evaluator;
  16. template < typename IntegerType >
  17. class lcm_evaluator;
  18. template < typename IntegerType >
  19. constexpr IntegerType gcd( IntegerType const &a, IntegerType const &b );
  20. template < typename IntegerType >
  21. constexpr IntegerType lcm( IntegerType const &a, IntegerType const &b );
  22. template < typename IntegerType, typename... Args >
  23. constexpr IntegerType gcd( IntegerType const &a, IntegerType const &b, Args const&... );
  24. template < typename IntegerType, typename... Args >
  25. constexpr IntegerType lcm( IntegerType const &a, IntegerType const &b, Args const&... );
  26. template <typename I>
  27. std::pair<typename std::iterator_traits<I>::value_type, I>
  28. gcd_range(I first, I last);
  29. template <typename I>
  30. std::pair<typename std::iterator_traits<I>::value_type, I>
  31. lcm_range(I first, I last);
  32. typedef ``['see-below]`` static_gcd_type;
  33. template < static_gcd_type Value1, static_gcd_type Value2 >
  34. struct static_gcd;
  35. template < static_gcd_type Value1, static_gcd_type Value2 >
  36. struct static_lcm;
  37. }
  38. }
  39. [endsect]
  40. [section GCD Function Object]
  41. [*Header: ] [@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>]
  42. template < typename IntegerType >
  43. class boost::integer::gcd_evaluator
  44. {
  45. public:
  46. // Types
  47. typedef IntegerType result_type;
  48. typedef IntegerType first_argument_type;
  49. typedef IntegerType second_argument_type;
  50. // Function object interface
  51. constexpr result_type operator ()(
  52. first_argument_type const &a,
  53. second_argument_type const &b ) const;
  54. };
  55. The boost::integer::gcd_evaluator class template defines a function object
  56. class to return the greatest common divisor of two integers.
  57. The template is parameterized by a single type, called IntegerType here.
  58. This type should be a numeric type that represents integers.
  59. The result of the function object is always nonnegative, even if either of
  60. the operator arguments is negative.
  61. This function object class template is used in the corresponding version of
  62. the GCD function template. If a numeric type wants to customize evaluations
  63. of its greatest common divisors, then the type should specialize on the
  64. gcd_evaluator class template.
  65. Note that these function objects are `constexpr` in C++14 and later only.
  66. They are also declared `noexcept` when appropriate.
  67. [endsect]
  68. [section LCM Function Object]
  69. [*Header: ] [@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>]
  70. template < typename IntegerType >
  71. class boost::integer::lcm_evaluator
  72. {
  73. public:
  74. // Types
  75. typedef IntegerType result_type;
  76. typedef IntegerType first_argument_type;
  77. typedef IntegerType second_argument_type;
  78. // Function object interface
  79. constexpr result_type operator ()(
  80. first_argument_type const &a,
  81. second_argument_type const &b ) const;
  82. };
  83. The boost::integer::lcm_evaluator class template defines a function object
  84. class to return the least common multiple of two integers. The template
  85. is parameterized by a single type, called IntegerType here. This type
  86. should be a numeric type that represents integers. The result of the
  87. function object is always nonnegative, even if either of the operator
  88. arguments is negative. If the least common multiple is beyond the range
  89. of the integer type, the results are undefined.
  90. This function object class template is used in the corresponding version
  91. of the LCM function template. If a numeric type wants to customize
  92. evaluations of its least common multiples, then the type should
  93. specialize on the lcm_evaluator class template.
  94. Note that these function objects are constexpr in C++14 and later only.
  95. They are also declared `noexcept` when appropriate.
  96. [endsect]
  97. [section:run_time Run-time GCD & LCM Determination]
  98. [*Header: ] [@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>]
  99. template < typename IntegerType >
  100. constexpr IntegerType boost::integer::gcd( IntegerType const &a, IntegerType const &b );
  101. template < typename IntegerType >
  102. constexpr IntegerType boost::integer::lcm( IntegerType const &a, IntegerType const &b );
  103. template < typename IntegerType, typename... Args >
  104. constexpr IntegerType gcd( IntegerType const &a, IntegerType const &b, Args const&... );
  105. template < typename IntegerType, typename... Args >
  106. constexpr IntegerType lcm( IntegerType const &a, IntegerType const &b, Args const&... );
  107. template <typename I>
  108. std::pair<typename std::iterator_traits<I>::value_type, I>
  109. gcd_range(I first, I last);
  110. template <typename I>
  111. std::pair<typename std::iterator_traits<I>::value_type, I>
  112. lcm_range(I first, I last);
  113. The boost::integer::gcd function template returns the greatest common
  114. (nonnegative) divisor of the two integers passed to it.
  115. `boost::integer::gcd_range` is the iteration of the above gcd algorithm over a
  116. range, returning the greatest common divisor of all the elements. The algorithm
  117. terminates when the gcd reaches unity or the end of the range. Thus it also
  118. returns the iterator after the last element inspected because this may not be
  119. equal to the end of the range. The variadic version of `gcd` behaves similarly
  120. but does not indicate which input value caused the gcd to reach unity.
  121. The boost::integer::lcm function template returns the least common
  122. (nonnegative) multiple of the two integers passed to it.
  123. As with gcd, there are range and variadic versions of the function for
  124. more than 2 arguments.
  125. Note that these functions are constexpr in C++14 and later only.
  126. They are also declared `noexcept` when appropriate.
  127. [endsect]
  128. [section:compile_time Compile time GCD and LCM determination]
  129. [note These functions are deprecated in favor of constexpr `gcd` and `lcm` on C++14 capable compilers.]
  130. [*Header: ] [@../../../../boost/integer/common_factor_ct.hpp <boost/integer/common_factor_ct.hpp>]
  131. typedef ``['unspecified]`` static_gcd_type;
  132. template < static_gcd_type Value1, static_gcd_type Value2 >
  133. struct boost::integer::static_gcd : public mpl::integral_c<static_gcd_type, implementation_defined>
  134. {
  135. };
  136. template < static_gcd_type Value1, static_gcd_type Value2 >
  137. struct boost::integer::static_lcm : public mpl::integral_c<static_gcd_type, implementation_defined>
  138. {
  139. };
  140. The type `static_gcd_type` is the widest unsigned-integer-type that is supported
  141. for use in integral-constant-expressions by the compiler. Usually this
  142. the same type as `boost::uintmax_t`, but may fall back to being `unsigned long`
  143. for some older compilers.
  144. The boost::integer::static_gcd and boost::integer::static_lcm class templates
  145. take two value-based template parameters of the ['static_gcd_type] type
  146. and inherit from the type `boost::mpl::integral_c`.
  147. Inherited from the base class, they have a member /value/
  148. that is the greatest common factor or least
  149. common multiple, respectively, of the template arguments.
  150. A compile-time error will occur if the least common multiple
  151. is beyond the range of `static_gcd_type`.
  152. [h3 Example]
  153. #include <boost/integer/common_factor.hpp>
  154. #include <algorithm>
  155. #include <iterator>
  156. #include <iostream>
  157. int main()
  158. {
  159. using std::cout;
  160. using std::endl;
  161. cout << "The GCD and LCM of 6 and 15 are "
  162. << boost::integer::gcd(6, 15) << " and "
  163. << boost::integer::lcm(6, 15) << ", respectively."
  164. << endl;
  165. cout << "The GCD and LCM of 8 and 9 are "
  166. << boost::integer::static_gcd<8, 9>::value
  167. << " and "
  168. << boost::integer::static_lcm<8, 9>::value
  169. << ", respectively." << endl;
  170. int a[] = { 4, 5, 6 }, b[] = { 7, 8, 9 }, c[3];
  171. std::transform( a, a + 3, b, c, boost::integer::gcd_evaluator<int>() );
  172. std::copy( c, c + 3, std::ostream_iterator<int>(cout, " ") );
  173. }
  174. [endsect]
  175. [section:gcd_header Header <boost/integer/common_factor.hpp>]
  176. This header simply includes the headers
  177. [@../../../../boost/integer/common_factor_ct.hpp <boost/integer/common_factor_ct.hpp>]
  178. and [@../../../../boost/integer/common_factor_rt.hpp <boost/integer/common_factor_rt.hpp>].
  179. Note this is a legacy header: it used to contain the actual implementation,
  180. but the compile-time and run-time facilities
  181. were moved to separate headers (since they were independent of each other).
  182. [endsect]
  183. [section:demo Demonstration Program]
  184. The program [@../../../../libs/integer/test/common_factor_test.cpp common_factor_test.cpp] is a demonstration of the results from
  185. instantiating various examples of the run-time GCD and LCM function
  186. templates and the compile-time GCD and LCM class templates.
  187. (The run-time GCD and LCM class templates are tested indirectly through
  188. the run-time function templates.)
  189. [endsect]
  190. [section Rationale]
  191. The greatest common divisor and least common multiple functions are
  192. greatly used in some numeric contexts, including some of the other
  193. Boost libraries. Centralizing these functions to one header improves
  194. code factoring and eases maintenance.
  195. [endsect]
  196. [section:gcd_history History]
  197. * 24th April 2017 Moved to Jeremy Murphy's improved algorithms, added constexpr and noexcept support,
  198. added compiler intrinsic support, added variadic and range based versions of the algorithms.
  199. * 13 May 2013 Moved into main Boost.Math Quickbook documentation.
  200. * 17 Dec 2005: Converted documentation to Quickbook Format.
  201. * 2 Jul 2002: Compile-time and run-time items separated to new headers.
  202. * 7 Nov 2001: Initial version
  203. [endsect]
  204. [section:gcd_credits Credits]
  205. The author of the Boost compilation of GCD and LCM computations is
  206. Daryle Walker. The code was prompted by existing code hiding in the
  207. implementations of Paul Moore's rational library and Steve Cleary's
  208. pool library. The code had updates by Helmut Zeisel.
  209. [endsect]
  210. [endsect]
  211. [/
  212. Copyright 2005, 2013 Daryle Walker.
  213. Distributed under the Boost Software License, Version 1.0.
  214. (See accompanying file LICENSE_1_0.txt or copy at
  215. http://www.boost.org/LICENSE_1_0.txt).
  216. ]