Mega Code Archive

 
Categories / C# / Data Types
 

Divides one BigInteger value by another, returns the result, and returns the remainder in an output parameter

using System; using System.Numerics; public class Example {    public static void Main()    {       BigInteger divisor = BigInteger.Pow(Int64.MaxValue, 2);       BigInteger[] dividends = { BigInteger.Multiply((BigInteger) Single.MaxValue, 2),                                   BigInteger.Parse("9999999999999999999999999999999999999"),                                   BigInteger.One,                                   BigInteger.Multiply(Int32.MaxValue, Int64.MaxValue),                                  divisor + BigInteger.One };       // Divide each dividend by divisor in three different ways.       foreach (BigInteger dividend in dividends)       {          BigInteger quotient;          BigInteger remainder = 0;          Console.WriteLine(BigInteger.Divide(dividend, divisor));          Console.WriteLine(dividend / divisor);          quotient = BigInteger.DivRem(dividend, divisor, out remainder);          Console.WriteLine("   Using DivRem method:     {0:N0}, remainder {1:N0}", quotient, remainder);       }                } }