Click here to Skip to main content
Click here to Skip to main content

An introduction to Big Integer class - Dotnet 4.0

, 26 Oct 2010
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Niladri_Biswas
Software Developer (Senior)
India India
Working as a s/w engineer in HCL

Comments and Discussions

 
GeneralMy vote of 1 PinmemberBatzen28-Oct-10 1:51 
GeneralMy vote of 1 PinmemberSledgeHammer0127-Oct-10 7:25 
GeneralMy vote of 1 PinmemberArgyle4Ever27-Oct-10 4:25 
GeneralMy vote of 2 PinmemberDaniel Brännström26-Oct-10 9:44 
GeneralMy vote of 2 PinmemberPeteSon_Q26-Oct-10 5:34 
GeneralMy vote of 1 PinmemberSelvin26-Oct-10 4:31 
GeneralIt is BigInteger, not BIG INTEGER Pinmemberleppie26-Oct-10 1:58 
GeneralRe: It is BigInteger, not BIG INTEGER PinmemberNiladri_Biswas26-Oct-10 3:52 
GeneralMy vote of 1 Pinmemberleppie26-Oct-10 1:57 
GeneralRe: My vote of 1 PinmemberNiladri_Biswas26-Oct-10 23:54 
GeneralRe: My vote of 1 Pinmemberleppie27-Oct-10 0:29 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 27 Oct 2010
Article Copyright 2010 by Niladri_Biswas
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid