BigInteger.GreatestCommonDivisor(BigInteger, BigInteger) 方法

定义

查找两个 BigInteger 值的最大公约数。Finds the greatest common divisor of two BigInteger values.

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

第一个值。The first value.

right
BigInteger

第二个值。The second value.

返回

BigInteger

leftright 的最大公约数。The greatest common divisor of left and right.

示例

下面的示例演示对方法的调用 GreatestCommonDivisor ,以及提供有关的有用信息所需的异常处理 ArgumentOutOfRangeExceptionThe following example illustrates a call to the GreatestCommonDivisor method and the exception handling necessary to provide useful information about an ArgumentOutOfRangeException. 结果指示这两个数字的最大公因数为1。The result indicates that the greatest common divisor of these two numbers is 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 不返回余数的情况下,可以将两个值相除为的最大数目。The greatest common divisor is the largest number into which the two BigInteger values can be divided without returning a remainder.

如果 leftright 参数为非零数字,则此方法始终返回至少1个值,因为所有数字都可以除以1。If the left and right parameters are non-zero numbers, the method always returns at least a value of 1 because all numbers can be divided by 1. 如果任一参数为零,则该方法将返回非零参数的绝对值。If either parameter is zero, the method returns the absolute value of the non-zero parameter. 如果两个值都为零,则该方法返回零。If both values are zero, the method returns zero.

备注

计算非常大的值的最大公约数 left ,这 right 可能是一项非常耗时的操作。Computing the greatest common divisor of very large values of left and right can be a very time-consuming operation.

无论和参数的符号如何,方法返回的值 GreatestCommonDivisor 始终为正值 left rightThe value returned by the GreatestCommonDivisor method is always positive regardless of the sign of the left and right parameters.

适用于