BigInteger.GreatestCommonDivisor(BigInteger, BigInteger) Método

Definición

Busca el máximo común divisor de dos valores 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

Parámetros

left
BigInteger

Primer valor.

right
BigInteger

Segundo valor.

Devoluciones

Máximo común divisor de left y right.

Ejemplos

En el ejemplo siguiente se muestra una llamada al GreatestCommonDivisor método y el control de excepciones necesario para proporcionar información útil sobre un ArgumentOutOfRangeException. El resultado indica que el divisor más común de estos dos números es 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

Comentarios

El divisor más común es el mayor número en el que se pueden dividir los dos BigInteger valores sin devolver un resto.

Si los left parámetros y right son números distintos de cero, el método siempre devuelve al menos un valor de 1 porque todos los números se pueden dividir entre 1. Si cualquiera de los parámetros es cero, el método devuelve el valor absoluto del parámetro distinto de cero. Si ambos valores son cero, el método devuelve cero.

Nota

Calcular el mayor divisor común de valores muy grandes de left y right puede ser una operación muy lenta.

El valor devuelto por el GreatestCommonDivisor método siempre es positivo, independientemente del signo de los left parámetros y right .

Se aplica a