tags:

views:

145

answers:

6

how to handle integers having say 25 digits in c++ solve the problems..

+6  A: 

Typically using a big-number library such as GNU MP or bigint.

unwind
Too easy ;) I suspect the challenge is to make the step from base-10 thinking to base-256 thinking or even, god forbid, BCD.
Lazarus
Your answer is good for the general case. But this is the wrong answer for a homework question.
Martin York
+1  A: 

"The Art of Computer Programming" by Donald Knuth, volume two "Seminumerical algorithms" has some useful info, if you're planning to implement it yourself rather than use a library function.

Kinopiko
A: 

Google revealed:

Arjen Lenstra's LIP package

Victor Shoup's NTL packaage

Jacob
A: 

Try using MAMP

http://www.tc.umn.edu/~ringx004/mapm-main.html

Pavels
A: 

I did a similar thing at Uni in Pascal to add and subtract extrememly large numbers. Simply you need to handle any number above the limit of your numbers, then any overflow goes into a larger bucket. The reverse applies (though you need to be more careful) for subtraction.

If you want to handle multiplication and division, and you don't want to use a third party package, then I refer you to a copy of Mike Abrash's Zen of Graphics Programming. In it he lists 32bit Multiplication and Divisions using 16bit numbers. It's in assembler, but you can handle that can't you?-)

graham.reeds
+2  A: 

In elementary school you probably learned how to do basic 4 operations (+-*/) manually on paper. Kids treat numbers as sequence of digits and process digit-by-digit.

(at least kids in Poland, where I grew up, used to learn it when I was young)

This method works for any big numbers. Adding 25-digit-long numbers works the same as adding 3-digit-long, only slower.

You have to recall how was it done when you were a kid and write a computer program that processes numbers in this way.

For extra performance and programmer credibility write it using base 65536 rather than base 10.

Tadeusz A. Kadłubowski
Buts its so much easier to write in ascii with std::vector<char> :-)
Martin York
@Martin: std::vector<unsigned short int>? Anyway, the optimal solution seems to be either base 2**machine_word_length or base 2**(machine_word_length/2). Needs to be benchmarked on the target CPU.
Tadeusz A. Kadłubowski