BigInteger.GreatestCommonDivisor(BigInteger, BigInteger) 方法

定义

查找两个 BigInteger 值的最大公约数。

public:
 static System::Numerics::BigInteger GreatestCommonDivisor(System::Numerics::BigInteger left, System::Numerics::BigInteger right);
public static System.Numerics.BigInteger GreatestCommonDivisor (System.Numerics.BigInteger left, System.Numerics.BigInteger right);
static member GreatestCommonDivisor : System.Numerics.BigInteger * System.Numerics.BigInteger -> System.Numerics.BigInteger
Public Shared Function GreatestCommonDivisor (left As BigInteger, right As BigInteger) As BigInteger

参数

left
BigInteger

第一个值。

right
BigInteger

第二个值。

返回

BigInteger

leftright 的最大公约数。

示例

下面的示例演示了对 GreatestCommonDivisor 方法的调用以及提供有关该 ArgumentOutOfRangeException方法的有用信息的异常处理。 结果表示这两个数字中最常见的除数为 1。

BigInteger n1 = BigInteger.Pow(154382190, 3);
BigInteger n2 = BigInteger.Multiply(1643590, 166935);
try
{
   Console.WriteLine("The greatest common divisor of {0} and {1} is {2}.",
                     n1, n2, BigInteger.GreatestCommonDivisor(n1, n2));
}
catch (ArgumentOutOfRangeException e)
{
   Console.WriteLine("Unable to calculate the greatest common divisor:");
   Console.WriteLine("   {0} is an invalid value for {1}",
                     e.ActualValue, e.ParamName);
}
Dim n1 As BigInteger = BigInteger.Pow(154382190, 3)
Dim n2 As BigInteger = BigInteger.Multiply(1643590, 166935)
Try
   Console.WriteLine("The greatest common divisor of {0} and {1} is {2}.", _
                     n1, n2, BigInteger.GreatestCommonDivisor(n1, n2))
Catch e As ArgumentOutOfRangeException
   Console.WriteLine("Unable to calculate the greatest common divisor:")
   Console.WriteLine("   {0} is an invalid value for {1}", _
                     e.ActualValue, e.ParamName)
End Try

注解

最常见的除数是可除以两 BigInteger 个值的最大数,而不返回余数。

left如果参数right为非零数字,则该方法始终至少返回一个值 1,因为所有数字都可以除以 1。 如果任一参数为零,该方法将返回非零参数的绝对值。 如果这两个值均为零,该方法将返回零。

备注

计算非常大值的 left 最常见除数, right 并且可能是一个非常耗时的操作。

无论参数的符号left如何right,方法返回GreatestCommonDivisor的值始终为正值。

适用于