23

For example I have a factorial program that needs to save really huge integers that can be 50+ digits long. The absolute maximum primitive data type in C++ is unsigned long long int with a maximum value 18446744073709551615 which is only 20 digits long. Here's the link to the limits of C++: http://www.cplusplus.com/reference/climits/

How do I store numbers that are larger than that in a variable of some sort?

1
  • 4
    By using a bigint library. GNU GMP is a good one to begin with. Commented Aug 5, 2013 at 14:07

4 Answers 4

23

If you already have a boost dependency (which many people these days do), you can use the boost multi-precision library. In fact, it already has an example of a factorial program that can support output up to 128 bits, though extending it further is pretty trivial.

Sign up to request clarification or add additional context in comments.

1 Comment

Kudos for pointing out to that one, I didn't know boost had grown such.
10

You'll have to use a bigint or bignum implementation. There are some libraries out there like this: http://gmplib.org/

Some more info and a list of libraries: http://en.wikipedia.org/wiki/Bignum

Comments

6

You can use array. First you should copy that giant number array,then use comma after 19 digits:

unsigned long long int num[]= { 7316717653133062491,9225119674426574742,3553491949349698352,0,312774506326239578,3180169848018694788, 5184385861560789112,9494954595017379583,3195285320880551112,5406987471585238630,5071569329096329522, 7443043557668966489,5044524452316173185,6403098711121722383,1136222989342338030,8135336276614282806, 4444866452387493035,8907296290491560440,7723907138105158593,0,7960866701724271218,8399879790879227492, 1901699720888093776,6572733300105336788,1220235421809751254,5405947522435258490,7711670556013604839, 5864467063244157221,5539753697817977846,1740649551492908625,6932197846862248283,9722413756570560574, 9026140797296865241,4535100474821663704,8440319989000889524,3450658541227588666,8811642717147992444, 2928230863465674813,9191231628245861786,6458359124566529476,5456828489128831426,0,7690042242190226710, 5562632111110937054,4217506941658960408,0,7198403850962455444,3629812309878799272,4428490918884580156, 1660979191338754992,0,0,5240636899125607176,0,6058861164671094050,7754100225698315520,0,0, 5593572972571636269,5618826704282524836,0,0,8232575304207529634,50}; 

4 Comments

why comma after 19 digits?
Because of size of long long int
The other solution is bigint or bignum implementation
stackoverflow.com/q/1055661/8678385 You can find the BigInt library here.
0

There are many ways to store very big number which are below:

  1. string
  2. File
  3. Link list
  4. Vector/Dynamic array

Note: Please don't use array to avoid memory problem issues.

2 Comments

How about static array or global array?
static array or global array is not good option to go because if continuous memory is not available then you can't store very big number.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.