extended_euclidean.qbk 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. [section:extended_euclidean Extended Euclidean Algorithm]
  2. [section Introduction]
  3. The extended Euclidean algorithm solves the integer relation /mx + ny/ = gcd(/m/, /n/) for /x/ and /y/.
  4. [endsect]
  5. [section Synopsis]
  6. #include <boost/integer/extended_euclidean.hpp>
  7. namespace boost { namespace integer {
  8. template<class Z>
  9. struct euclidean_result_t {
  10. Z gcd;
  11. Z x;
  12. Z y;
  13. };
  14. template<class Z>
  15. euclidean_result_t<Z> extended_euclidean(Z m, Z n);
  16. }}
  17. [endsect]
  18. [section Usage]
  19. int m = 12;
  20. int n = 15;
  21. auto res = extended_euclidean(m, n);
  22. int gcd = res.gcd;
  23. int x = res.x;
  24. int y = res.y;
  25. // mx + ny = gcd(m,n) should now hold
  26. [endsect]
  27. [section References]
  28. Wagstaff, Samuel S., ['The Joy of Factoring], Vol. 68. American Mathematical Soc., 2013.
  29. [endsect]
  30. [endsect]
  31. [/
  32. Copyright 2018 Nick Thompson.
  33. Distributed under the Boost Software License, Version 1.0.
  34. (See accompanying file LICENSE_1_0.txt or copy at
  35. http://www.boost.org/LICENSE_1_0.txt).
  36. ]