Arbitrarily Large Numbers

Hi There, I've been going through the project Euler sets with c++, and am on problem 19. However, I ended up switching to python to complete problem 16:

2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 2^1000?

I did this as C++ can't really calculate arbitrarily large integers with default libraries. At least, not with some code beyond my skill level.

So I was wondering, what are some methods to calculate numbers such as these, using default libraries. I am hoping to have a universal solution, as I believe it is a safe bet that going through project Euler will require me to do this many times again. I don't necessarily want to copy and paste code, so a push in the right direction is appreciated, but then again, you're the experts.

Thanks,

TheWitchKing
Last edited on
A bignum is really just an array of integers that are chained together to represent a big number. The simplest versions are actually just character strings.

Addition and subtraction are as simple as a pairwise add with carry. Multiplication and division are not so simple -- they require a little more effort.

Here's an older thread on it:
http://www.cplusplus.com/forum/lounge/32041/

So -- the answer is that you will need a library of some sort no matter what. A nice simple one is TTMath, which is a templated header-file-only kind of library
http://www.ttmath.org/

Hope this helps.
It's not standard... But in Boost.

www.boost.org/doc/libs/1_53_0/libs/multiprecision/doc/html/boost_multiprecision/intro.html

There is a solution.
Thanks, Duoas!
It's not the answer I was hoping for, but TTMath looks pretty promising for my purposes.

Thanks,
TheWitchKing
Topic archived. No new replies allowed.