Find the greatest common divisor of two BigInteger values in CSharp

Description

The following code shows how to find the greatest common divisor of two BigInteger values.

Example


using System;//ww w .  java 2 s .c o  m
using System.Numerics;

public class Example
{
   public static void Main()
   {

        BigInteger n1 = BigInteger.Pow(9999999999, 3);
        BigInteger n2 = BigInteger.Multiply(9999999999, 99999999);
        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);
        }
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    Data Types »




C# Data Types
Bool
Byte
Char
Decimal
Double
Float
Integer
Long
Short
String
C# Array
Array Example
Byte Array
C# Standard Data Type Format
BigInteger
Complex
Currency
DateTime
DateTimeOffset
DateTime Format Parse Convert
TimeSpan
TimeZone
Enum
Null
tuple
var