-2

I want to store and operate on very large integers, what is the best way to do this without using pre-built libraries?

Based on what another StackOverflow user stated:

The std::string object will be copied onto the stack, but the string body will not - it will be allocated on heap. The actual limitation will depend on the system and program memory usage and can be something like from ten million to one billion charactes on a 32-bit system.

I just thought of two simple ways, both of which require me to write my own class. The first is to use vectors and strings, and the second is to break down a large integer into separate blocks in integer arrays and add up the total.

The max.size() of a string on my computer is 4294967291.

I have decided to write my own class. Thanks for the help: C++ char vector addition

EDIT: Working on it: https://github.com/Jyang772/Large_Number_Collider

Community
  • 1
  • 1
Quaxton Hale
  • 2,460
  • 5
  • 40
  • 71
  • 2
    And your question is? – Some programmer dude Nov 28 '13 at 08:42
  • 1
    It depends what your priorities are (simplicity of coding? ease of maintenance? performance? memory consumption?) and what operations you need to perform on them. – David Schwartz Nov 28 '13 at 08:42
  • 3
    Take a look at a pre-existing library and copy it. Mess around with its code until you understand it. – Benjamin Lindley Nov 28 '13 at 08:42
  • If you want to operate on large integers, storing strings won't be the best solution. Ie, how do you want to add/subtract/divide/... strings? – Axel Nov 28 '13 at 08:42
  • Possible duplicate of http://stackoverflow.com/questions/269268/how-to-implement-big-int-in-c – JBL Nov 28 '13 at 08:42
  • or just use the library! – Mitch Wheat Nov 28 '13 at 08:42
  • 1
    The short answer is this -- you do it the same way you do it on pencil and paper! – David Schwartz Nov 28 '13 at 08:43
  • @Axel, MitchWheat Thanks for being practical. But, you know what...Challenge Accepted. Just for the hell of it, and because I actually want to learn something and not just use a pre-built library like I have ben doing in the past. I'll edit my question as soon as I am done. Getting neg-repped for this is pretty bad. :L – Quaxton Hale Nov 28 '13 at 08:57
  • Steve Skiena gives away a simple C implementation of what you're after: http://www3.cs.stonybrook.edu/~skiena/392/programs/bignum.c – MatthewD Sep 20 '14 at 13:45

3 Answers3

1

If depends on the usage of this integer, but to keep the semantic of numbers and make your class coding easier, i'd suggest to use a vector of long integers. Using std::string will be far more complicated for code design and maintenance.

You will have to redefine every operators and take into account the propagation of computations from one chunk of you number to an other.

Oragon Efreet
  • 1,114
  • 1
  • 8
  • 25
  • Thank you. I liked your answer because you did not assume this was for homework. I have been taking a look at BigInt and decided to write my own class. I'll probably just edit this question later with my completed code. – Quaxton Hale Nov 28 '13 at 08:51
0

The usual way is to use an array (vector, etc) of ints (longs, etc), rather than a string.

Start by looking at existing large integer classes, even if you can't use one verbatim for your homework.

Will Dean
  • 39,055
  • 11
  • 90
  • 118
  • Thank you. And this isn't for homework, why would you assume that? Is it because of my age or because you think I'm looking for source code to be written for me? I have decided to write my own class. It will be more complicated but challenge accepted. I actually want to learn something I'm interested in. – Quaxton Hale Nov 28 '13 at 09:04
  • Ok, I apologise - it's just that unexplained requirements NOT to use ready made components are usually a sign of homework (which isn't an insult by the way, most people have done some in their lives!). If you're just a hard-working guy who's keen to learn, then make sure you communicate that in your questions. – Will Dean Nov 28 '13 at 09:07
0

When we faced similar problems on contests, we used vectors, each cell containing one digit of the number. This way you can store an immensely large number.

Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76