0

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

Lazarus
  • 41,906
  • 4
  • 43
  • 54
d3vdpro
  • 2,887
  • 4
  • 25
  • 29
  • You can use this library. http://mattmccutchen.net/bigint/ – vpram86 Oct 13 '09 at 11:52
  • @Aviator: I found that independently, but I recommend posting answers as answers, not comments. – unwind Oct 13 '09 at 12:07
  • Since this is for homework I guess the user will want to generate it self and simply telling them to use a 3rd party library probably won't get their work marked. – graham.reeds Oct 13 '09 at 12:10
  • @unwind: I too thought of putting it as answer. But as graham suggested, i felt that it might not be a good way to solve his purpose. He can take it as a last resort! So just left as a comment. – vpram86 Oct 13 '09 at 12:14

6 Answers6

6

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

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    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 Oct 13 '09 at 11:55
  • Your answer is good for the general case. But this is the wrong answer for a homework question. – Martin York Oct 13 '09 at 19:00
2

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
  • 8,047
  • 1
  • 30
  • 37
1

"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.

0

Google revealed:

Arjen Lenstra's LIP package

Victor Shoup's NTL packaage

Community
  • 1
  • 1
Jacob
  • 34,255
  • 14
  • 110
  • 165
0

Try using MAMP

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

Pavels
  • 1,256
  • 1
  • 12
  • 19
0

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
  • 16,230
  • 17
  • 74
  • 137