BigInteger.GreatestCommonDivisor(BigInteger, BigInteger) Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Encontra o maior divisor comum de dois BigInteger valores.
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
O primeiro valor.
- right
- BigInteger
O segundo valor.
Devoluções
O maior divisor comum de left e right.
Exemplos
O exemplo seguinte ilustra uma chamada ao GreatestCommonDivisor método e o tratamento de exceções necessário para fornecer informações úteis sobre um ArgumentOutOfRangeException. O resultado indica que o maior divisor comum destes dois números é 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
Observações
O maior divisor comum é o maior número em que os dois BigInteger valores podem ser divididos sem devolver um resto.
Se os left parâmetros e right forem números não nulos, o método devolve sempre pelo menos um valor de 1 porque todos os números podem ser divididos por 1. Se algum parâmetro for zero, o método devolve o valor absoluto do parâmetro não nulo. Se ambos os valores forem zero, o método devolve zero.
Note
Calcular o maior divisor comum de valores muito grandes de left e right pode ser uma operação muito demorada.
O valor devolvido pelo GreatestCommonDivisor método é sempre positivo (incluindo zero) independentemente do sinal dos left parâmetros e.right