4

I am trying to do pow(2,500) in C++. But I think long long is not enough.

Someone told me I can use gmp.h. But how do I do a pow(2,500) in gmp?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JJ Liu
  • 1,351
  • 8
  • 20
  • 36
  • Already answered. Try [this answer](http://stackoverflow.com/a/7749847/968261) for example. – Alexey Frunze Jan 08 '12 at 01:39
  • possible duplicate of [storing more than 2 power 31 on a 32-bit system](http://stackoverflow.com/questions/7651229/storing-more-than-2-power-31-on-a-32-bit-system) – Alexey Frunze Jan 08 '12 at 01:40
  • @Alex: Sorry, your answer is not suitable for the question, which asks for a GMP-based solution. (Why? Because GMP is much more memory-efficient than your solution.) A decimal-expansion-based solution is okay if you are in a programming contest where you can't use external libraries. Otherwise, it's a really suboptimal approach. – C. K. Young Jan 08 '12 at 03:42
  • @Alex: More specifically, in a decimal-expansion solution, you are using a whole char to store 3.322 bits of data. That means you have a 58.5% wastage in space. – C. K. Young Jan 08 '12 at 03:48
  • @ChrisJester-Young: it's trivial to make that code use only 4 bits per digit, right? – Alexey Frunze Jan 08 '12 at 08:07
  • @Alex: Yes. That is called BCD (binary-coded decimal), which is from the era of EBCDIC and the like. It's evil. :-P (I meant that in a tongue-pokey way, BTW...sort of.) It's not as wasteful ("only" 17% wastage), but, well, you know my opinion on BCD, so. – C. K. Young Jan 08 '12 at 08:10

1 Answers1

4

See Integer Exponentiation. Hint: choose either of the bottom two functions.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 1
    That's probably a comment and not quite an answer, but the again the OP isn't quite a question... – Kerrek SB Jan 08 '12 at 01:33
  • @KerrekSB: It actually is an answer: the bottom two functions listed on that page will do exactly as required. If you think it's better to write that into the answer, that can be done, but is less ideal, because I want to encourage the OP to do some reading of their own. – C. K. Young Jan 08 '12 at 01:37