2018-01-28 14:47:14 -06:00
|
|
|
/*
|
|
|
|
* (C) Copyright Nick Thompson 2018.
|
|
|
|
* Use, modification and distribution are subject to the
|
|
|
|
* Boost Software License, Version 1.0. (See accompanying file
|
|
|
|
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#ifndef BOOST_INTEGER_EXTENDED_EUCLIDEAN_HPP
|
|
|
|
#define BOOST_INTEGER_EXTENDED_EUCLIDEAN_HPP
|
|
|
|
#include <limits>
|
2018-10-24 14:29:22 -06:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <boost/throw_exception.hpp>
|
2018-01-28 14:47:14 -06:00
|
|
|
|
|
|
|
namespace boost { namespace integer {
|
|
|
|
|
2018-10-25 14:28:39 -06:00
|
|
|
// From "The Joy of Factoring", Algorithm 2.7, with a small optimization to remove tmps from Wikipedia.
|
2018-01-28 14:47:14 -06:00
|
|
|
// Solves mx + ny = gcd(m,n). Returns tuple with (gcd(m,n), x, y).
|
2018-10-25 09:38:16 -06:00
|
|
|
|
|
|
|
template<class Z>
|
|
|
|
struct euclidean_result_t {
|
|
|
|
Z gcd;
|
|
|
|
Z x;
|
|
|
|
Z y;
|
|
|
|
};
|
|
|
|
|
2018-01-28 14:47:14 -06:00
|
|
|
template<class Z>
|
2018-10-25 09:38:16 -06:00
|
|
|
euclidean_result_t<Z> extended_euclidean(Z m, Z n)
|
2018-01-28 14:47:14 -06:00
|
|
|
{
|
|
|
|
using std::numeric_limits;
|
|
|
|
static_assert(numeric_limits<Z>::is_integer,
|
|
|
|
"The extended Euclidean algorithm works on integral types.\n");
|
|
|
|
|
|
|
|
static_assert(numeric_limits<Z>::is_signed,
|
|
|
|
"The extended Euclidean algorithm only works on signed integer types.\n");
|
|
|
|
|
|
|
|
if (m < 1 || n < 1)
|
|
|
|
{
|
2018-10-24 14:29:22 -06:00
|
|
|
BOOST_THROW_EXCEPTION(std::domain_error("Arguments must be strictly positive.\n"));
|
2018-01-28 14:47:14 -06:00
|
|
|
}
|
2018-10-25 14:28:39 -06:00
|
|
|
|
2018-10-25 18:05:14 -06:00
|
|
|
bool swapped = false;
|
|
|
|
if (m < n)
|
|
|
|
{
|
|
|
|
swapped = true;
|
|
|
|
std::swap(m, n);
|
|
|
|
}
|
|
|
|
Z u0 = m;
|
|
|
|
Z u1 = 1;
|
|
|
|
Z u2 = 0;
|
|
|
|
Z v0 = n;
|
|
|
|
Z v1 = 0;
|
|
|
|
Z v2 = 1;
|
|
|
|
Z w0;
|
|
|
|
Z w1;
|
|
|
|
Z w2;
|
|
|
|
while(v0 > 0)
|
|
|
|
{
|
|
|
|
Z q = u0/v0;
|
|
|
|
w0 = u0 - q*v0;
|
|
|
|
w1 = u1 - q*v1;
|
|
|
|
w2 = u2 - q*v2;
|
|
|
|
u0 = v0;
|
|
|
|
u1 = v1;
|
|
|
|
u2 = v2;
|
|
|
|
v0 = w0;
|
|
|
|
v1 = w1;
|
|
|
|
v2 = w2;
|
2018-02-09 17:19:26 -06:00
|
|
|
}
|
|
|
|
|
2018-10-25 18:05:14 -06:00
|
|
|
if (swapped)
|
|
|
|
{
|
2018-10-26 19:23:11 -06:00
|
|
|
return {u0, u2, u1};
|
2018-10-25 18:05:14 -06:00
|
|
|
}
|
|
|
|
return {u0, u1, u2};
|
2018-01-28 14:47:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}}
|
|
|
|
#endif
|