1 Star 0 Fork 63

雁秋 / C-Sharp

forked from 编程语言算法集 / C-Sharp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
ExtendedEuclideanAlgorithm.cs 3.61 KB
一键复制 编辑 原始数据 按行查看 历史
using System.Numerics;
namespace Algorithms.ModularArithmetic
{
/// <summary>
/// Extended Euclidean algorithm: https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm.
/// </summary>
public static class ExtendedEuclideanAlgorithm
{
/// <summary>
/// Computes the greatest common divisor (gcd) of integers a and b, also the coefficients of Bézout's identity,
/// which are integers x and y such that a*bezoutCoefficientOfA + b*bezoutCoefficientOfB = gcd(a, b).
/// </summary>
/// <param name="a">Input number.</param>
/// <param name="b">Second input number.</param>
/// <returns>A record of ExtendedEuclideanAlgorithmResult containing the bezout coefficients of a and b as well as the gcd(a,b).</returns>
public static ExtendedEuclideanAlgorithmResult<long> Compute(long a, long b)
{
long quotient;
long tmp;
var s = 0L;
var bezoutOfA = 1L;
var r = b;
var gcd = a;
var bezoutOfB = 0L;
while (r != 0)
{
quotient = gcd / r; // integer division
tmp = gcd;
gcd = r;
r = tmp - quotient * r;
tmp = bezoutOfA;
bezoutOfA = s;
s = tmp - quotient * s;
}
if (b != 0)
{
bezoutOfB = (gcd - bezoutOfA * a) / b; // integer division
}
return new ExtendedEuclideanAlgorithmResult<long>(bezoutOfA, bezoutOfB, gcd);
}
/// <summary>
/// Computes the greatest common divisor (gcd) of integers a and b, also the coefficients of Bézout's identity,
/// which are integers x and y such that a*bezoutCoefficientOfA + b*bezoutCoefficientOfB = gcd(a, b).
/// </summary>
/// <param name="a">Input number.</param>
/// <param name="b">Second input number.</param>
/// <returns>A record of ExtendedEuclideanAlgorithmResult containing the bezout coefficients of a and b as well as the gcd(a,b).</returns>
public static ExtendedEuclideanAlgorithmResult<BigInteger> Compute(BigInteger a, BigInteger b)
{
BigInteger quotient;
BigInteger tmp;
var s = BigInteger.Zero;
var bezoutOfA = BigInteger.One;
var r = b;
var gcd = a;
var bezoutOfB = BigInteger.Zero;
while (r != 0)
{
quotient = gcd / r; // integer division
tmp = gcd;
gcd = r;
r = tmp - quotient * r;
tmp = bezoutOfA;
bezoutOfA = s;
s = tmp - quotient * s;
}
if (b != 0)
{
bezoutOfB = (gcd - bezoutOfA * a) / b; // integer division
}
return new ExtendedEuclideanAlgorithmResult<BigInteger>(bezoutOfA, bezoutOfB, gcd);
}
/// <summary>
/// The result type for the computation of the Extended Euclidean Algorithm.
/// </summary>
/// <typeparam name="T">The data type of the computation (i.e. long or BigInteger).</typeparam>
/// <param name="bezoutA">The bezout coefficient of the parameter a to the computation.</param>
/// <param name="bezoutB">The bezout coefficient of the parameter b to the computation.</param>
/// <param name="gcd">The greatest common divisor of the parameters a and b to the computation.</param>
public record ExtendedEuclideanAlgorithmResult<T>(T bezoutA, T bezoutB, T gcd);
}
}
C#
1
https://gitee.com/xyesterday/C-Sharp.git
git@gitee.com:xyesterday/C-Sharp.git
xyesterday
C-Sharp
C-Sharp
master

搜索帮助