Return integer with zero signaling common factor rather than boost::optional<Z>.

This commit is contained in:
Nick Thompson
2018-12-04 10:55:03 -07:00
parent cad4623876
commit 51b259da19
3 changed files with 16 additions and 18 deletions

View File

@ -14,7 +14,7 @@ A fast algorithm for computing modular multiplicative inverses based on the exte
namespace boost { namespace integer {
template<class Z>
boost::optional<Z> mod_inverse(Z a, Z m);
Z mod_inverse(Z a, Z m);
}}
@ -22,20 +22,19 @@ A fast algorithm for computing modular multiplicative inverses based on the exte
[section Usage]
Multiplicative modular inverses exist if and only if /a/ and /m/ are coprime.
So for example
int x = mod_inverse(2, 5);
// prints x = 3:
std::cout << "x = " << x << "\n";
auto x = mod_inverse(2, 5);
if (x)
{
int should_be_three = x.value();
}
auto y = mod_inverse(2, 4);
if (!y)
int y = mod_inverse(2, 4);
if (y == 0)
{
std::cout << "There is no inverse of 2 mod 4\n";
}
Multiplicative modular inverses exist if and only if /a/ and /m/ are coprime.
If /a/ and /m/ share a common factor, then `mod_inverse(a, m)` returns zero.
[endsect]
[section References]