A simple BigNum library for .NET

Update

Due to minor demand, the code is also available: BigNum source.

Please note that I haven’t actually touched the code since it was first written. I’m sure there’s some things that don’t work properly. If you’re doing anything big with this you probably want to write some ‘destructive’ update functions for adding, etc. At the moment every time you add, subtract, etc, a completely new BigInt is returned. To make it faster you’d want to just update the number in-place.

Original content…

I’ve created a simple wrapper for GMP. At the moment only BigInts are implemented, but I expect to have BigRationals and BigFloats coming along soon enough. (Edit: this never happened.)

Usage

I think this is very close to as-you’d-expect. The only minor thing that might come as a surprise is that BigInts aren’t value types; since they need destructors, they can’t be. The rest is fairly straightforward:

static void Main(string[] args) {
    BigInt n;
    for (ulong i = 100; i <= 999; i++) {
        n = BigInt.Factorial(i);
        Console.WriteLine(n);
    }
    Console.ReadLine();
}

This code executes with sub 5-second times, which I’m pretty pleased with. I’m not sure what kind of performance decrease you get through using GMP under managed code.

Caveats

  • Consider this alpha software
  • The included GMP DLL is compiled with none of the magical assembly they provide. If you want to, you can compile it yourself with all this enabled, and you should be able to just use it as a drop-in replacement.

Download

Available here: BigNum library. Documentation is available.

Comments 7

  1. Brent wrote:

    Great! I hope this works. I’ve been looking for something like this for a long time. I’m initially planning to use it with the Boo .net programming language, exploring the Collatz problem.

    Posted 19 Mar 2008 at 6:52 am
  2. BigNum library wrote:

    Hello,

    I’m writing to ask if you can send me the code itself not only the dll for BigNum project? Currently I am trying to write a program which will calculate large amounts of Pi digits and find any pattern in it. I would be very thankful, since I don’t remember when I last wrote something in C++ (recently only c#). In my case time is a
    key issue.

    Please excuse me if I wrote something unpleasant. My English is not on a high level. If you are interested I am a Polish Warsaw University of Technology student.

    Regards

    Dawid Rutyna

    Posted 13 Apr 2008 at 1:28 pm
  3. Bart wrote:

    Hey, i’m playing with the code, and i found that you assumed that ‘signed long int’ is 64 bit, but the gmp.dll is 32 bit ? so the code starts failing for values larger then 2^32

    Posted 23 Dec 2008 at 4:18 am
  4. Porges wrote:

    Hi Bart,

    Good point, I never wrote any testing code for this, it was more a proof of concept

    At the moment I don’t have much interest in updating this, but if you fix the code feel free to tell me and I’ll link to your version.

    Thanks!

    Posted 23 Dec 2008 at 1:23 pm
  5. Robin wrote:

    Hi Porges, I hope you’re still active?
    I use Visual Studio 2008 for Visual Basic .. I can add the BigNum dll as reference (include the dll~) but at runtime it says it can’t use gmp.dll because it’s not a valid assembly or COM component :S I need BigNum for Visual Basic to handle RSA cryptography

    Posted 04 Aug 2009 at 11:48 pm
  6. Porges wrote:

    Robin,

    A better route to take might be to have a look at the .NET 4 beta that is currently out; it has support for BigInteger arithmetic built-in (under System.Numerics).

    You could download the .NET 4 beta and use System.Numerics.dll, then just distribute that DLL with your code.

    As to the rest of your question, there is already support for RSA built into the .NET framework, or are you trying to write your own implementation as an exercise?

    Posted 06 Aug 2009 at 9:26 pm
  7. Mr Amazing wrote:

    Just an FYI, but if anyone’s interested in a decimal / floating-point library for C#/VB/.NET I found “W3b.Sine” which is under the BSD license. http://codeplex.com/sine

    Posted 19 Sep 2009 at 9:34 am

Post a Comment

Your email is never published nor shared. Required fields are marked *