Using BigInteger with C#

My previous posting was about performance of Fibonacci numbers algorithms. In this posting I will introduce you some problems related to limits of our usual integers and introduce you new feature in .Net Framework 4.0 – big integers. Big integers are useful when solving different mathematical problems. Also they are used in cryptography.

As a first thing let’s see the problem we may face when we use our usual integers. Let’s use faster implementation of Fibonacci numbers algorithm so we don’t have to wait a long time to get results we need.

public int Fibonacci(int x)
{
   
var
previousValue = -1;
   
var
currentResult = 1;

   
for (var
i = 0; i <= x; ++i)
    {
       
var
sum = currentResult + previousValue;
        previousValue = currentResult;
        currentResult = sum;
    }

   
return currentResult;
}
InputActualExpected
40102334155102334155
41165580141165580141
42267914296267914296
43433494437433494437
44701408733701408733
4511349031701134903170
4618363119031836311903
47-13237522232971215073
485125596804807526976
49-8111925437778742049
50-29863286312586269025

Now let’s take some value range where we can expect big numbers as result. Let’s take range from 40 to 50 and compare the results of our algorithm with expected results.

We can see that starting from 47 our results are different. 47 is the point where we go over integer limits and then we go to “next round” in integer scope. Same thing happens also with unsigned integer, long and unsigned long.

If we take some bigger input for our algorithm then we will get even larger numbers. 47 is small number and I am sure that scientific calculations use usually bigger inputs too and breaking over the limits of used type is almost sure.

You may also consider some other algorith that produces faster growth of results. Perfect candidate for this is factorial algorithm. By example 40! = 815915283247897734345611269596115894272000000000.

Introducing BigInteger class

.Net Framework 4.0 solves our problem. New namespace System.Numerics contains class called BigInteger. You can use objects based on this class as usual integers. This class also provides static methods for different mathematical operations on big integers. Let’s move now to BigInteger class with our Fibonacci numbers algorithm.

public BigInteger Fibonacci(int x)
{
   
var previousValue = new
BigInteger(-1);
   
var currentResult = new
BigInteger(1);

   
for (var
i = 0; i <= x; ++i)
    {
       
var
sum = currentResult + previousValue;
        previousValue = currentResult;
        currentResult = sum;
    }

   
return currentResult;
}

When we run this code we get expected results.

Some words about BigInteger structure

BigInteger is structure by its nature. It has bunch of static methods that let you perform different mathematical operations on BigIntegers. There are also many operators and type conversions defined for BigInteger so you can use BigIntegers like usual integers. Take look at the BigInteger members documentation.

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    One thought on “Using BigInteger with C#

    • March 12, 2010 at 4:17 am
      Permalink

      Although .NET BigInteger provides fewer methods than Java’s, the .NET Framework 4 BigInteger is atleast twice faster than Java for the operations like addition, subtraction, multiplication, division, etc in my stress tests on the same machine.

    Leave a Reply

    Your email address will not be published. Required fields are marked *