How can I use bigint with C#?

47,999

Solution 1

You can use System.Numerics.BigInteger (add a reference to System.Numerics assembly). As mentioned in the comments this might not be the right approach though.

Solution 2

Here's using BigInteger. This method Prints Numbers in the Fibonacci Sequence up to n.

public static void FibonacciSequence(int n)
{
    /** BigInteger easily holds the first 1000 numbers in the Fibonacci Sequence. **/
    List<BigInteger> fibonacci = new List<BigInteger>();
    fibonacci.Add(0);
    fibonacci.Add(1);
    BigInteger i = 2;
    while(i < n)
    {                
        int first = (int)i - 2;
        int second = (int) i - 1;
        BigInteger firstNumber =  fibonacci[first];
        BigInteger secondNumber = fibonacci[second];
        BigInteger sum = firstNumber + secondNumber;
        fibonacci.Add(sum);
        i++;
    }         
    foreach (BigInteger f in fibonacci) { Console.WriteLine(f); }
}

Solution 3

Native support for big integers has been introduced in .NET 4.0. Just add an assembly reference to System.Numerics, add a using System.Numerics; declaration at the top of your code file, and you’re good to go. The type you’re after is BigInteger.

Solution 4

BigInteger is available in .NET 4.0 or later. There are some third-party implementations as well (In case you are using an earlier version of the framework).

Solution 5

Better use System.Numerics.BigInteger.

Share:
47,999
Ersin Gulbahar
Author by

Ersin Gulbahar

Technologies : Big Data, Informatica, Apache Hadoop, Apache Kafka, Confluent, Elasticsearch, Apache Sqoop, NoSql, Openshift Programming Languages : PYTHON, JAVA, T-SQL, PL-SQL, C#, Unity Databases : Oracle, MsSQL, Couchbase, HBASE, PostgreSQL, Vertica, Terradata, SAP Hana, MySQL Stackoverflow : https://stackoverflow.com/users/1332870/ersin-g%c3%bclbahar Dzone : https://dzone.com/users/4487476/ersingulbahar.html Github : https://github.com/ersingulbahar

Updated on July 09, 2020

Comments

  • Ersin Gulbahar
    Ersin Gulbahar over 3 years

    I work to implement an RSA key algorithm. But I couldn't use a 2048-bit value. How I can use it?

    I want to use big integer.