views:

375

answers:

7

I'm doing some Project Euler problems and most of the time, the computations involve large numbers beyond int, float, double etc.

Firstly, I know that I should be looking for more efficient ways of calculation so as to avoid the large number problem. I've heard of the Bignum libraries.

But, for academics interests, I'd like to know how to code my own solution to this problem.

Can any expert please help me out? (My language is C)

+1  A: 

Here's a nice and simple bignum module for C. You can learn from it for ideas. The C code isn't the highest quality, but the algorithm is well implemented and quite common.

For more advanced stuff, look up GMP.

Eli Bendersky
+6  A: 

You need to store the big numbers in a base that your computer can easily handle with its native types, and then store the digits in a variable length array. I'd suggest that for simplicity you start by storing the numbers in base 10 just to get the hang of how to do this. It will make debugging a lot easier.

Once you have a class that can store the numbers in this form, it's just a matter of implementing the operations add, subtract, multiply, etc. on this class. Each operation will have to iterate over digits of its operands and combine them, being careful to carry correctly so that your digits are never larger than the base. Addition and subtraction are simple. Multiplication requires a bit more work as the naive algorithm requires nested loops. Then once you have that working, you can try implementing exponentiation in an efficient manner (e.g. repeated squaring).

If you are planning to write a serious bignum implementation, base 10 won't cut it. It's wasteful of memory and it will be slow. You should choose a base that is natural for the computer, such as 256 or the word size (2**32). However this will make simple operations more difficult as you will get overflows if you naively add two digits, so you will need to handle that very carefully.

Mark Byers
Usually, you would choose a base that is half the largest representable type so that there are no overflows. For instance, in modern C, you would choose `uint32_t` and do all the digit arithmetic in `uint64_t`. And don't forget: make the digit type `unsigned` so that there are no surprises.
jk
Actually, base 10 is entirely adequate. It all depends on your purposes. If speed is paramount, then convolution is a great way to do a multiply, IF you have a fast convolver. And convolution will overflow if you use a large base.
woodchips
+1  A: 

A simple way is to think of the number as its string representation in base b. Suppose b=10, simple arithmetic operation like addition on two such strings can be done using the same method we use when adding numbers by pen and paper. The same goes for other simple operations. For better results, you can take a larger base.

A simple bignum implementation like that should be enough for most Project Euler problems (probably all, but I haven't solved much at Euler so can't be sure), but there are ways of using much faster algorithms for operations such as multiplication and division/mod.

Although I recommend writing your own bignum for practice, if you are really stuck you can take ideas from the code of already implemented bigint libraries. For a serious implementation something like gmp is the obvious choice. But you cana also find small bigints coded by other people when solving similar practice problem online (e.g. Abednego's bigint.cpp).

MAK
A: 

If you want a nice C++ version (I know, you said C, but this is really interesting code), take a look at the internals of CGAL: http://www.cgal.org/

Andrew McGregor
+8  A: 
Roger Pate
+1 Good answer.
JesperE
I don't disagree with what you have to say, but the OP wants to learn how to implement bigints.
MAK
And I'm not saying he shouldn't learn (at some point) how they are implemented either (plus other answers cover that for him, no sense repeating it). But, in the context from the question, writing his own bigint library is not even an appropriate solution, much less a candidate for a good one.
Roger Pate
+2  A: 

A popular bignum library for C/C++ is the GNU MP Bignum Library. I've used it for several Project Euler problems, but fact remains that C isn't a very suitable language for Euler-problems. If performance was more important C would have more to give, but now you're much better off using a language which built in bignum support, such as Ruby (there are lots of others).

JesperE
What's important in Project Euler is what you choose to make important. I did a bunch of problems when I was learning C++, and my objective was not to solve each one in 2 minutes, but to solve *all the ones I'd done* in 2 minutes total ;-)
Steve Jessop
+2  A: 

I totally agree with Roger Pate. I have seen many times people running into integer limit issue with C/C++/Java, but with Python, it's a non-issue. For most Project Euler problems, coming up with the right algorithm is most important, and the performance you get from C won't matter much. Furthermore with the associative data types, dictionary, set, etc. that are available in Python and some of the built-in libraries, itertools, just to name one, it's much faster to solve a problem with Python. I started seriously learning Python since I jumped on the Project Euler bandwagon and I have been happy with my decision (my first language is C++, second language is Perl, but I wanted to learn Python).

grokus