How to calculate the GCD (greatest common divisor) of two BigIntegers in C#

1 Answer

0 votes
using System;
using System.Numerics;

public class Program
{
	public static void Main()
	{
		BigInteger bigInteger1 = BigInteger.Parse("3679523593914784257459000");
		BigInteger bigInteger2 = BigInteger.Parse("274372696650 ");
		
		try {
  			Console.WriteLine("Greatest common divisor: " + BigInteger.GreatestCommonDivisor(bigInteger1, bigInteger2));
		}
		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);
		}
	}
}



/*
run:

Greatest common divisor: 150

*/

 



answered Jun 9, 2024 by avibootz
...