How to store extremely large numbers?

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?


Solution 1:

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.

Solution 2:

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

Solution 3:

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};