BigInteger.GreatestCommonDivisor(BigInteger, BigInteger) Metod

Definition

Hittar den största gemensamma nämnaren av två BigInteger värden.

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

Parametrar

left
BigInteger

Det första värdet.

right
BigInteger

Det andra värdet.

Returer

Den största gemensamma nämnaren för left och right.

Exempel

I följande exempel visas ett anrop till GreatestCommonDivisor metoden och den undantagshantering som krävs för att ge användbar information om en ArgumentOutOfRangeException. Resultatet anger att den största gemensamma nämnaren för dessa två tal är 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

Kommentarer

Den största gemensamma nämnaren är det största talet där de två BigInteger värdena kan delas upp utan att returnera en rest.

Om parametrarna left och right inte är nolltal returnerar metoden alltid minst ett värde på 1 eftersom alla tal kan divideras med 1. Om någon av parametrarna är noll returnerar metoden det absoluta värdet för parametern non-zero. Om båda värdena är noll returnerar metoden noll.

Note

Att beräkna den största gemensamma nämnaren för mycket stora värden i left och right kan vara en mycket tidskrävande åtgärd.

Värdet som returneras av GreatestCommonDivisor metoden är alltid positivt (inklusive noll) oavsett tecknet för parametrarna left och right .

Gäller för