Project Description

This an implementation of a BigInteger and includes full C# source in a single file. The implementation supports BigIntegers of arbitrary size and is compatible with C# 2.0 (and might also work with earlier compiler releases). Objects support all of the usual arithmetic and relational operators.

I originally wrote this for C# 1.1 and I could (and should) now drop the lame internal representation of the "byte" array and replace it with a generic argument to the internal class that manages the byte array. I wrote things this way because I had different scenarios where the average size of the integer was vastly different and I wanted to be sure I could change the internal representation based on my final performance requirements.

For a description of the algorithms, see D. Knuth, The Art of Computer Programming, Volume 2.

Example

public class MainProgram
{
    public static void Main(string[] args)
    {
        BigInteger a = new BigInteger(25);
        a = a + 100;

        BigInteger b = new BigInteger("139435810094598308945890230913");

        BigInteger c = b / a;
        BigInteger d = b % a;

        BigInteger e = (c * a) + d;
        if (e != b)
        {
            Console.WriteLine("Can never be true.");
        }
    }
}


Usage

I would just cut and paste the source code from BigInteger.cs into your own project and change the name space ScottGarland.BigInteger to whatever you want. The DType will replaced with a generic parameter to the DigitsArray class in the next release. If you want to change the internal arithmetic base type - by default it's System.UInt32 - look at the first part of the source in BigInteger.cs and you'll see a using statement with some commented alternatives.

Last edited Sep 13, 2007 at 2:01 PM by scgarland, version 17