Click here to Skip to main content
15,886,741 members
Articles / Programming Languages / C# 4.0

An introduction to Big Integer class - Dotnet 4.0

Rate me:
Please Sign up or sign in to vote.
3.30/5 (15 votes)
26 Oct 2010CPOL2 min read 38K   191   9   11
This short article will show some of the benifits that the BigInteger class provides

Introduction

BigInteger class that resides in the System.Numerics namespace helps in representing any large integer without any loss of precision. It has been introduced earlier in dotnet framework 3.5 but was removed(don’t know the exact reason). However, it is again back in framework 4.0

Using the code

Example 1: To find the factorial of 100.

static void Main(string[] args)
{
    Console.Write("Factorial of hundred is :  ");
    HundredFactorial();
    Console.Read();
}

/// <summary />
/// Function: HundredFactorial
/// Factorial of Hundred
/// </summary />
private static void HundredFactorial()
{
    BigInteger number = 100;
    BigInteger fact = 1;
    for (; number-- > 0; ) fact *= number+1;
    Console.WriteLine(fact);
}

Output:

2.jpg

The program is pretty simple only. We have used the new Go To operator for looping through the elements in order to get the factorial.

Example 2:Find the Sum Of First One Lac Even FibonacciSeries

static void Main(string[] args)
{
Console.WriteLine("The Sum Of First One Lac Even FibonacciSeries is : ");
    Console.WriteLine(Environment.NewLine);
    SumOfFirstOneLacEvenFibonacciSeries();
    Console.Read();
}

/// <summary />
/// Function: SumOfFirstOneLacEvenFibonacciSeries
/// To find the Sum Of First One Lac Even FibonacciSeries
/// </summary />
private static void SumOfFirstOneLacEvenFibonacciSeries()
{
    int limit = 100000;

    BigInteger num1 = 1;
    BigInteger num2 = 2;
    BigInteger sum = 0;
    BigInteger evenSum = num1 + num2;
    for (int i = 2; i < limit; i++)
    {
        sum =  num1 + num2;
        if (sum % 2 == 0) evenSum += sum;
        num1 = num2;
        num2 = sum;
    }

    Console.WriteLine(evenSum);

}

A Fibonacci series goes like 1,2,3,5,8,11...etc.

i.e. 1+2 =3, 2+3 =5, 3+5 = 8 etc.

The program finds the Fibonacci number till one lac and gives the sum of only the even Fibonacci numbers.

The output being

4.jpg

Some BigIntMethods

a)GreatestCommonDivisor

Program to find the GCM of two numbers

Console.WriteLine("</span />GCM = "</span /> +  BigInteger.GreatestCommonDivisor(12</span />, 24</span />));

Output is: 12

b)Compare

Compares two System.Numerics.BigInteger values and returns an integer that indicates whether the first value is less than, equal to, or greater than the second value.

BigInteger num1 = 10;
BigInteger num2 = 100;

switch (BigInteger.Compare(num1, num2))
{
  case -1: Console.WriteLine("{0} is less than {1}", num1, num2);
           break;
  case 0: Console.WriteLine("{0} is equal to  {1}", num1, num2);
           break;
  case 1: Console.WriteLine("{0} is greater then  {1}", num1, num2);
          break;
}

c)Parse

Converts the string representation of a number to its System.Numerics.BigInteger equivalent.e.g.

BigInteger.Parse("</span />10000000"</span />)

d)Negate

Negates a specified System.Numerics.BigInteger value.e.g.

Console.WriteLine(BigInteger.Negate(-100</span />));

results in 100 and

Console.WriteLine(BigInteger.Negate(100</span />));

results in -100

e)Sign Read property.

Gets a number that indicates the sign (negative, positive, or zero) of the current System.Numerics.BigInteger object. e.g.

Console.WriteLine(BigInteger.Negate(0</span />).Sign);

will give 0

Console.WriteLine(BigInteger.Negate(-1</span />).Sign);

will give 1

While

Console.WriteLine(BigInteger.Negate(1</span />).Sign);

will give -1

Like these, there are many properties and methods of this class that helps to do very big mathematical calculations.

Type cast

Conversion of BigInteger to standard numeric type and viceversa are both possible.

//Conversion(int to BigInterger)
 int i = 100;
 BigInteger bI = (BigInteger)i;
 Console.WriteLine(bI);

 //Conversion (BigInteger to int)
 BigInteger BI = 200;
 int j = (int)BI;
 Console.WriteLine(j);
 Console.Read();

Conclusion:

In this article we have seen some of the usage of BigInteger class, its usefulness in larger number calculations and some of the built-in methods and properties of this class.

Comments on the topic are highly appreciated for the improvement of the topic.

Thanks for reading the article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralMy vote of 1 Pin
Batzen28-Oct-10 1:51
Batzen28-Oct-10 1:51 
GeneralMy vote of 1 Pin
SledgeHammer0127-Oct-10 7:25
SledgeHammer0127-Oct-10 7:25 
GeneralMy vote of 1 Pin
Argyle4Ever27-Oct-10 4:25
Argyle4Ever27-Oct-10 4:25 
GeneralMy vote of 2 Pin
Daniel Brännström26-Oct-10 9:44
Daniel Brännström26-Oct-10 9:44 
GeneralMy vote of 2 Pin
PeteSon_Q26-Oct-10 5:34
PeteSon_Q26-Oct-10 5:34 
GeneralMy vote of 1 Pin
Selvin26-Oct-10 4:31
Selvin26-Oct-10 4:31 
GeneralIt is BigInteger, not BIG INTEGER Pin
leppie26-Oct-10 1:58
leppie26-Oct-10 1:58 
GeneralRe: It is BigInteger, not BIG INTEGER Pin
Niladri_Biswas26-Oct-10 3:52
Niladri_Biswas26-Oct-10 3:52 
GeneralMy vote of 1 Pin
leppie26-Oct-10 1:57
leppie26-Oct-10 1:57 
GeneralRe: My vote of 1 Pin
Niladri_Biswas26-Oct-10 23:54
Niladri_Biswas26-Oct-10 23:54 
I have removed the picture code now and put the real one. Thanks for your advice
Niladri Biswas

GeneralRe: My vote of 1 Pin
leppie27-Oct-10 0:29
leppie27-Oct-10 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.