BigInteger in C++

Lately, I’m trying to code few basic problems in C++ and finding it hard in some cases when problems with BigInteger arises.

With Java, I can easily do,
BigInteger b = new BigInteger(“111111111111111111111111111111111111111”);

That above value could not be even held in a long datatype in java.

Similarly If I want to do the same in C++, Is there any straightforward piece of code [expecting in 2 to 3 lines], such that the usage becomes easy.

P.S : Couldn’t find anything useful in google / codeforces - one such blog had hectic big explanation and didn’t understand a thing :frowning:

you can try the following library in c++
instructions to use it are also present in readme file.

1 Like

Thanks! :slight_smile:

But the code is too large and still didn’t understand. Need to spend a day or two to sit and understand how BigInteger works properly before using it blindly.

1 Like

Use Boost Multiprecision Library.

It’s as simple as including a header file :wink:

Include these lines in your code!

#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;

Now simply use your BigInt like this

cpp_int a;

Thanks a lot :slight_smile:

You can use this , little more documented (still incomplete, but does most jobs).
P.S. I don’t own the codes. All I own is documentation.

1 Like

@s5960r what is the range of this “cpp_int” ?

thanks , this is working fine
tested for this problem — solution

It stores numbers like an array internally maybe…so it’s pretty big🤣 probably like 10^6 or 10^7

Be careful tho… All arithmetic operations would take O(n) where n is number of digits

yes O(n*n) for multiplication
we cannot avoid that unless if we can implement Karatsuba

1 Like

I think it is much more. You can calculate 20! that is upto 18+ length

No i mean… 10^7 digits :rofl:

1 Like

Python is the solution :joy:


LOL, seeing c++ and python solution’s makes me think those languages are Rocket science’s :P.

For example, see this code :


@aryanc403 :slight_smile: