共用方式為


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

第二個值。

傳回

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);
}
let n1 = BigInteger.Pow(154382190, 3)
let n2 = BigInteger.Multiply(1643590, 166935)

try
    printfn $"The greatest common divisor of {n1} and {n2} is {BigInteger.GreatestCommonDivisor(n1, n2)}."
with :? ArgumentOutOfRangeException as e ->
    printfn $"Unable to calculate the greatest common divisor:"
    printfn $"   {e.ActualValue} is an invalid value for {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。 如果任一參數為零,則方法會傳回非零參數的絕對值。 如果這兩個值都是零,方法會傳回零。

注意

計算非常大型值的leftright最常見除數,可能是非常耗時的作業。

不論 和 right 參數的left正負號為何,方法所GreatestCommonDivisor傳回的值一律為正數。

適用於