BigInteger.GreatestCommonDivisor(BigInteger, BigInteger) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
找到兩個 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
第二個值。
傳回
與left的最大right公因數。
範例
以下範例說明了對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。 若任一參數為零,方法會回傳非零參數的絕對值。 若兩個值皆為零,則該方法回傳零。
Note
計算 和 left 非常大值right的最大公因數可能是一項非常耗時的操作。
方法回傳GreatestCommonDivisor的值無論參數left的符號right如何,都永遠是正值(包括零)。