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
you can try the following library in c++
link
instructions to use it are also present in readme file.
1 Like
Thanks!
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
s5960r
November 3, 2019, 2:53pm
4
Use Boost Multiprecision Library.
It’s as simple as including a header file
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;
7 Likes
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
s5960r
November 3, 2019, 11:41pm
9
@anon8049083
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
s5960r
November 4, 2019, 5:26am
12
@koulick_424
No i mean… 10^7 digits
1 Like
LOL, seeing c++ and python solution’s makes me think those languages are Rocket science’s :P.
For example, see this code : CodeChef: Practical coding for everyone
Woof!!!