How to store extremely large numbers? - c++

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?

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.

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

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

There are many ways to store very big number which are below:
string
File
Link list
Vector/Dynamic array
Note: Please don't use array to avoid memory problem issues.

Related

How to handle a integer value bigger than the max int(64) can store? [duplicate]

I am trying to write a program for finding Mersenne prime numbers. Using the unsigned long long type I was able to determine the value of the 9th Mersenne prime, which is (2^61)-1. For larger values I would need a data type that could store integer values greater than 2^64.
I should be able to use operators like *, *=, > ,< and % with this data type.
You can not do what you want with C natives types, however there are libraries that let handle arbitrarily large numbers, like the GNU Multiple Precision Arithmetic Library.
To store large numbers, there are many choices, which are given below in order of decreasing preferences:
1) Use third-party libraries developed by others on github, codeflex etc for your mentioned language, that is, C.
2) Switch to other languages like Python which has in-built large number processing capabilities, Java, which supports BigNum, or C++.
3) Develop your own data structures, may be in terms of strings (where 100 char length could refer to 100 decimal digits) with its custom operations like addition, subtraction, multiplication etc, just like complex number library in C++ were developed in this way. This choice could be meant for your research and educational purpose.
What all these people are basically saying is that the 64bit CPU will not be capable of adding those huge numbers with just an instruction but you rather need an algorithm that will be able to add those numbers. Such an algorithm would have to treat the 2 numbers in pieces.
And the libraries they listed will allow you to do that, a good exercise would be to develop one yourself (just the algorithm/function to learn how it's done).
There is no standard way for having data type greater than 64 bits. You should check the documentation of your systems, some of them define 128 bits integers. However, to really have flexible size integers, you should use an other representation, using an array for instance. Then, it's up to you to define the operators =, <, >, etc.
Fortunately, libraries such as GMP permits you to use arbitrary length integers.
Take a look at the GNU MP Bignum Library.
Use double :)
it will solve your problem!

How to Print number which is over ULLONG_MAX at Console ?

I want to print "845100400152152934331135470251" or "1071292029505993517027974728227441735014801995855195223534251"
but in C++ the max value of "Unsigned long long " is "18446744073709551615"
this is much less than which I want to print
please help me...
First of all, your problem is not about printing big numbers but storing them in variables (and maybe calculating on them).
On some compilers (GCC for example), you have variable types like int128 that can handle numbers up to 10^38 (more less).
If this doesn't solve the problem, you'll have to write your own arithmetic. For example, store numbers in strings and write functions that will calculate on them (addition and subtraction is rather easy, multiplying medium (as long as numbers aren't really huge), dividing by big integers hard). Alternatively you can look for already made big integer libraries (on the Internet, c++ doesn't have built-in one).

How to find the log of a number?

I have a number (num1) which is 18 digits long. I am storing it in an integer array.
I have another number (num2) which is also 18 digits long. This is also being stored in an integer array.
I have to find the log of the first number to the base of the second number (log num1 to base num2).
How to do this in C++? I can't use the log function as the numbers are being stored in arrays.
They key term to google for is bigint. There are various C++ libraries which support bigints (that is, number which can be as long as your memory permits).
The only bigint library I've used myself is GMP. However, if you just need a single function on bigints (log, in your case), then maybe taking some smaller library (is more practical).
Just checked the GMP page and incidentally, it calls them bignums all over. So that may be another useful term to use when searching for a solution. ;-)

how to sum two or three big number? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
how to sum a large number of float number?
“BigInt” in C?
handling integer having large number of digits
I want to sum two different number .
think we have two different number that length of anyone is more than 20 number , how can I sum both ? as far as I know , I can not do this with int .
like these :
26565468416574156465651652464234245645945643526 + 6264635454513215421541412154121541544455412315
There is a bunch of libraries that can do this as well as you may implement it yourself. Check this Wikipedia article.
Take a look at this C++ Big Integer lib
You must use some BigInteger implementation. Either search for a C++ library that does that or implement it yourself.
Most common implementations store the "big integer" in an array of bytes. To add two of those, do a byte-wise addition and take care of carry (both generate and properate).
EDIT: Not necessarily bytes. Any unsigned storage like int32, int64 or whatever your machine can handle.
Miracl is a great solution and pretty standard solution :
http://www.shamus.ie/index.php?page=Downloads

how do I declare an integer variable of 1024 bits in length?

I'm trying to write an algorithm for a number theory/computer science merged class that can factor large numbers in better than exponential time. I am using the g++ compiler on a 64 bit machine but when I chain together long it will only allow me to do up to 2 longs. Is there any way to tell it to use an arbitrary amount of space for a variable?
If you want just a collection of longs, you can declare an array of longs. But you don't want that. You want https://mattmccutchen.net/bigint/ BigIntegers :-)
Alternatives:
http://gmplib.org/
http://www.mpir.org/
(disclaimer: I haven't tested/used them)
Or if you want to implement them
How to implement big int in C++
I'll add that the C++ std library doesn't contain a big integer implementation (source STL big int class implementation )
You'll need a library. A good one is http://gmplib.org/