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 <tuple>
|
|
|
|
#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
|
|
|
|
|
|
|
Z s = 0;
|
|
|
|
Z old_s = 1;
|
|
|
|
Z r = n;
|
|
|
|
Z old_r = m;
|
|
|
|
|
|
|
|
while (r != 0) {
|
|
|
|
Z q = old_r/r;
|
|
|
|
Z tmp = r;
|
|
|
|
r = old_r - q*tmp;
|
|
|
|
old_r = tmp;
|
|
|
|
|
|
|
|
tmp = s;
|
|
|
|
s = old_s - q*tmp;
|
|
|
|
old_s = tmp;
|
2018-02-09 17:19:26 -06:00
|
|
|
}
|
|
|
|
|
2018-10-25 14:28:39 -06:00
|
|
|
Z y = (old_r - old_s*m)/n;
|
|
|
|
|
|
|
|
BOOST_ASSERT(old_s*m+y*n==old_r);
|
|
|
|
return {old_r, old_s, y};
|
2018-01-28 14:47:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}}
|
|
|
|
#endif
|