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();
}
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();
Console.Read();
}
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("GCM = " + BigInteger.GreatestCommonDivisor(12, 24));
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("10000000")
d)Negate
Negates a specified System.Numerics.BigInteger value.e.g.
Console.WriteLine(BigInteger.Negate(-100));
results in 100 and
Console.WriteLine(BigInteger.Negate(100));
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).Sign);
will give 0
Console.WriteLine(BigInteger.Negate(-1).Sign);
will give 1
While
Console.WriteLine(BigInteger.Negate(1).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.
int i = 100;
BigInteger bI = (BigInteger)i;
Console.WriteLine(bI);
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.