How can I store very very large integers accurately?

How to store large integers accurately?

2 Likes

Some problems require the use of what is called arbitrary-precision arithmetic, for example, adding together two numbers of 100 digits each.

These numbers are too large to store in an 32 or 64 bit integer. They are also too big to store in a double - a double is not accurate enough to store every single digit.

Some languages such as Java or Python have big integer classes built in. In other languages such as C/C++, the best way of handling these is to store them as strings. You can write methods to multiply, divide, add or subtract these just as you were taught in school using pen and paper.

9 Likes

Hello @rudreshwar,

I have written a tutorial that covers the representation of big numbers in C/C++ using arrays and then multiplication, and I applied it to the problem FCTRL…

Here is the link:

Big Numbers multiplication and representation

Maybe this can help you out! :slight_smile:

Best regards,

Bruno

9 Likes

If you are using c++, since it has no default support for very huge numbers, you can use one of the Big Integer classes that are already implemented or you can implement one yourself if you have time. I found a pretty good implementation called InfInt. It’s fast and very easy to use. I used it in a few problems on CodeChef and it worked. Check out this link if you are interested: Google Code Archive - Long-term storage for Google Code Project Hosting.

4 Likes

I have a good C++ code for BigNum:

link

4 Likes

If you integer is larger that maximum value of long data type e.g. 2^64 -1 then you can use BigInteger class in Java, which is just there represent to really large number. One good scenario is when you calculate the large factorials. You need BigInteger even to hold factorial of 50 accurately.

2 Likes

Uses python or java its help alot in challenges and in c/c++ use array if input individually index or string .

3 Likes

is c and c++ are better for competitive purpose than java or python?

1 Like

@arpa: your bignum is either wrong or incomplete. The code for the first function: minus is in itself not complete. Perhaps you linked to the wrong file?

1 Like

C and C++ are generally preferred for competitive programming as they 're much faster than Java and Python.

1 Like

we can deal with large integers in C++ as well ,using the boost library and codechef online judge also supports this
I have also used this to solve a question in which we had to compute factorial of a number as large as 100 , which is not possible if you use traditional datatypes such as long long int , you can veiw it if you wish
https://www.codechef.com/viewsolution/30983659

do note that this library is not supported by every online judge , For example : codechef and hackerearth online judge support this library but its not supported on codeforces online judge

1 Like

Use Double or Long , then use (new Double(double_value)).intValue() , it works typically !