Question

I've been toying around with some Project Euler problems and naturally am running into a lot that require the handling of bigger than long long type numbers. I am committed to using Cocoa and Objective-C (I need to stay sharp for work) but can't find an elegant way (read: library) to handle these really big numbers.

I'd love to use GMP but is sounds like using it with Xcode is a complete world of hurt.

Does anyone know of any other options?

Was it helpful?

Solution

If I were you I would compile gmp outside XCode and use just gmp.h and libgmp.a (or libgmp.dylib) in my XCode project.

OTHER TIPS

Try storing the digits in arrays.

Although you will have to write some new functions for all your arithmatic problems but thats how we were told to do it in college.

Plus the speed of calculations was pretty improved as big numbers weren't really big afterall and were not numbers really altogether

see if it helps

regards

vBigNum in vecLib implements 1024 bit integers (signed or unsigned). Is that big enough?

If you wanted to use matlab (or anything close) you could look at my implementation of a big integer form (vpi) on the file exchange.

It is rather simple. Store each digit separately. Adds and subtracts are simple, just implement a carry operation. Multiplies are best done using convolution, then a carry. Implement divide and mod operators, then a powermod operation, useful for many of the PE problems. Powers are easy - just repeated squaring and multiplication, based on the binary representation of the exponent.

This will let you solve many PE problems.

I too got the bright idea to attempt some Euler Project problems with Cocoa/Objective-C and have found it frustrating. I previously used Java and perhaps some PHP. I posted my exact problem in this thread.

I always considered using a library cheating for this project. Just write a class with the things you need. And don't be afraid to use malloc and uint64_t and so on. NSNumber is not a good idea in many cases.

On the other hand, there are many problems where the obvious solution would require huge to enormously huge numbers, and the trick is to find a way to solve the problem without using these huge numbers. (For example, what is the sum of the last thousand digits of 1,000,000 factorial)?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top