views:

279

answers:

7

Upon reaching a brick wall with the .Net framework's lack of a BigInteger class (yet), I've decided I'd like to develop my own as an exercise (I realize open source alternatives exist). What hoops do I need to jump through to be able to develop this? Is there any particuliar knowledge pieces that I probably wouldn't have?

edit: side question. Which data type would you use to represent the numbers inside of your new big integer class?

+7  A: 

Arbitrary precision arithmetic?

Edit: To represent your numbers you will probably want a resizeable array of integers.

Andy Balaam
Good call! Some interesting facts on the side in that article, too.
Chris
+6  A: 

I would brush up on your basic math skills. When I wrote a Big Int class I had to remember how to add, multiply and divide by hand like in Elementary school.

Next if you are going to create a new class I would try to follow the standards that have been set up for the Framework. So it looks like any other .Net class.

I would follow TDD so you know your class works the way it is designed.

David Basarab
Is it suggested that for this class I mock the algorithms after the way I'd perform these calculations on paper? If so, what data type do you start with/
Chris
A: 

Maybe studying an already implemented BigInteger class might help?

Sorin Comanescu
This was my first instinct. I realized there's a lot of hex and bit pushing going on, which I'm fairly dusty with. That's why I asked!
Chris
+3  A: 

You need to have a very good understanding of number systems. You could choose to represent the bignum in base 10, base 2 or any base x. This choice would affect your class performance a lot. You also have to choose the algorithms you want to implement. In general, great libraries like GMP for example, choose the algorithm based on the size of the operands. There are a lot of topics you have to be aware of, but in the end you should be convinced that you can't produce something really interesting. As a learning topic it is very valuable, but as producing something useful consider NOT reinventing the wheel!

AraK
Reinventing the wheel for an end product is definitely not my goal. This is entirely educational. I'm thinking it would be smartest to implement this in only base 10 for my first try at this, for simplicity sake, no?
Chris
Representing the number in base 10 might be good for displaying the numbers, but it's a nightmare if you need to make operation on it.
jpbochi
What would make it a nightmare for operations?
Chris
@jpbochi While I agree that base 2^n is the natural choice. Many good libraries like MAPM represents *big float* in base **100**. It is not a nightmare, it is a trade off between goals you want to achieve.
AraK
Your base type is in base 2, and you want to work in base 10.
Breton
+3  A: 

If you want to dive really deep into the math of it, you need to read Donald Knuth.

jpbochi
+2  A: 

.Net 3.5 has a BigInteger class, but it's scoped internal to the CLR. You can see the code using Reflector. Open System.Core.dll and look in the System.Numeric namespace. BigInteger is the only class in that namespace.

If you want to see the code for the F# BigInteger class, look in the [F# install folder]\source\fsharp\FSharp.Core\math\z.fs file.

Chris R. Timmons
A: 

If you use elementary algorithms, your BigInts will be unusable for very large integers, for example like Mersenne Primes.

If this is ok for you, use a simple 32 bit int as the basic data type. You need to handle 64 results in this case. If you don't care about speed at all, use some radix-10 number as base, lets say 10000, which will be very easy to implement.

This is mainly because the multiplication in a naive implementation has O(n^2) runtime. Advanced algorithms, based on fourier transforms, have O(n(log(n)) runtime. This requires some mathematical skills and knowledge.

drhirsch