mod_inverse.qbk 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. [section:mod_inverse Modular Multiplicative Inverse]
  2. [section Introduction]
  3. The modular multiplicative inverse of a number /a/ is that number /x/ which satisfies /ax/ = 1 mod /p/.
  4. A fast algorithm for computing modular multiplicative inverses based on the extended Euclidean algorithm exists and is provided by Boost.
  5. [endsect]
  6. [section Synopsis]
  7. #include <boost/integer/mod_inverse.hpp>
  8. namespace boost { namespace integer {
  9. template<class Z>
  10. Z mod_inverse(Z a, Z m);
  11. }}
  12. [endsect]
  13. [section Usage]
  14. int x = mod_inverse(2, 5);
  15. // prints x = 3:
  16. std::cout << "x = " << x << "\n";
  17. int y = mod_inverse(2, 4);
  18. if (y == 0)
  19. {
  20. std::cout << "There is no inverse of 2 mod 4\n";
  21. }
  22. Multiplicative modular inverses exist if and only if /a/ and /m/ are coprime.
  23. If /a/ and /m/ share a common factor, then `mod_inverse(a, m)` returns zero.
  24. [endsect]
  25. [section References]
  26. Wagstaff, Samuel S., ['The Joy of Factoring], Vol. 68. American Mathematical Soc., 2013.
  27. [endsect]
  28. [endsect]
  29. [/
  30. Copyright 2018 Nick Thompson.
  31. Distributed under the Boost Software License, Version 1.0.
  32. (See accompanying file LICENSE_1_0.txt or copy at
  33. http://www.boost.org/LICENSE_1_0.txt).
  34. ]