12,072,540 members (56,599 online)
alternative version

20.8K views
9 bookmarked
Posted

An introduction to Big Integer class - Dotnet 4.0

, 27 Oct 2010 CPOL
 Rate this:
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();
}

/// <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:

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();
}

/// <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

Some BigIntMethods

a)GreatestCommonDivisor

Program to find the GCM of two numbers

`Console.WriteLine("</span />GCM = "</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

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 />`

will give 0

`Console.WriteLine(BigInteger.Negate(-1</span />`

will give 1

While

`Console.WriteLine(BigInteger.Negate(1</span />`

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);
```

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.

You may also be interested in...

 First Prev Next
 My vote of 1 Batzen28-Oct-10 2:51 Batzen 28-Oct-10 2:51
 My vote of 1 SledgeHammer0127-Oct-10 8:25 SledgeHammer01 27-Oct-10 8:25
 My vote of 1 Argyle4Ever27-Oct-10 5:25 Argyle4Ever 27-Oct-10 5:25
 My vote of 2 Daniel Brännström26-Oct-10 10:44 Daniel Brännström 26-Oct-10 10:44
 My vote of 2 PeteSon_Q26-Oct-10 6:34 PeteSon_Q 26-Oct-10 6:34
 My vote of 1 Selvin26-Oct-10 5:31 Selvin 26-Oct-10 5:31
 It is BigInteger, not BIG INTEGER `leppie`26-Oct-10 2:58 `leppie` 26-Oct-10 2:58
 Re: It is BigInteger, not BIG INTEGER Niladri_Biswas26-Oct-10 4:52 Niladri_Biswas 26-Oct-10 4:52
 My vote of 1 `leppie`26-Oct-10 2:57 `leppie` 26-Oct-10 2:57
 Re: My vote of 1 `leppie`27-Oct-10 1:29 `leppie` 27-Oct-10 1:29