BigInteger.GreatestCommonDivisor(BigInteger, BigInteger) Metodo

Definizione

Trova il divisore comune più grande di due BigInteger valori.

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

Parametri

left
BigInteger

Primo valore.

right
BigInteger

Secondo valore.

Valori restituiti

Il più grande divisore comune di left e right.

Esempio

Nell'esempio seguente viene illustrata una chiamata al GreatestCommonDivisor metodo e la gestione delle eccezioni necessaria per fornire informazioni utili su un oggetto ArgumentOutOfRangeException. Il risultato indica che il divisore comune più grande di questi due numeri è 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

Commenti

Il divisore comune più grande è il numero più grande in cui i due BigInteger valori possono essere divisi senza restituire un resto.

Se i left parametri e right sono numeri diversi da zero, il metodo restituisce sempre almeno un valore pari a 1 perché tutti i numeri possono essere divisi per 1. Se uno dei due parametri è zero, il metodo restituisce il valore assoluto del parametro diverso da zero. Se entrambi i valori sono zero, il metodo restituisce zero.

Note

Il calcolo del divisore comune più grande di valori molto grandi di left e right può essere un'operazione molto dispendiosa in termini di tempo.

Il valore restituito dal GreatestCommonDivisor metodo è sempre positivo (incluso zero) indipendentemente dal segno dei left parametri e right .

Si applica a