1

How to calclute 2 to the power 10000000 without crashing the compiler. What shoud be data type for extramily big integer in c/c++.

Prashant
  • 394
  • 1
  • 6
  • 18
  • What do you want to do with the result, display it in binary? – Brent Bradburn Sep 11 '16 at 06:39
  • If you're working with large numbers, you could consider using boosts arbitrary precision integers. You can also look at the following post. http://stackoverflow.com/questions/15400031/c-what-variable-type-for-extremely-big-integer-numbers – Timothy Murphy Sep 11 '16 at 06:55
  • @Prashant - After your edit the title doesn't match the body. – Support Ukraine Sep 11 '16 at 07:12
  • Yep but it dosnt matter what is the power of 2. Its only too big – Prashant Sep 11 '16 at 07:19
  • Do you want to print the calculated result in decimal form? Or do you want to use the result for something else? Note: `2^10000000` will result in a little more than 3.000.000 decimal chars... – Support Ukraine Sep 11 '16 at 07:22
  • For further calculation – Prashant Sep 11 '16 at 07:28
  • I think there are multiple open source projects for this. Just download their code and study how they handle the internal representation of big integer numbers. Maybe this could be a start: https://gmplib.org/ – Support Ukraine Sep 11 '16 at 07:39
  • You may be able to take advantage of the fact that 2 to the power of 10000000 is just `1 << 10000000`. – user4581301 Sep 11 '16 at 08:05

3 Answers3

5

For the very specific value 2 raised to the power of 1000 a double is sufficient.

#include <stdio.h>
#include <math.h>

int main(int argc, const char *argv[]) {
    printf("%f\n", pow(2., 1000));
    return 0;
}

In general however you will need to implement an arbitrary precision multiplication algorithm to compute numbers that big (or use a library that provides that).

C++ has no predefined standard functions for this kind of computation.

If you want to implement your own version as an exercise then my suggestion is to use numbers in base 10000. They're small enough that single-digit multiplication won't overflow and it's very easy and fast to translate the result into decimal at the end because you can just map base-10000 digits to decimal without having to implement division an modulo too.

Also to compute such a big power (10,000,000) you will need to implement power by squaring, i.e.

BigNum pow(BigNum a, int b) {
    if (b == 0) {
        return 1;
    } else if (b & 1) {
        return a*pow(a, b-1);
    } else {
        BigNum x = pow(a, b/2);
        return x*x;
    }
}

this will allow to compute pow(a, b) with O(log(b)) instead of O(b) multiplications.

6502
  • 112,025
  • 15
  • 165
  • 265
  • 2
    This isn't recommended because the answer wouldn't be precise. http://stackoverflow.com/questions/7371928/c-pow2-1000-is-normaly-to-big-for-double-but-its-working-why – Abhirath Mahipal Sep 11 '16 at 06:47
  • 2
    @AbhirathMahipal: as I said it works for the very specific value of 2 raised to 1000. For that the result is correct. Please take the time of reading and understanding the answer you are linking to if you are not convinced. – 6502 Sep 11 '16 at 06:49
  • I'm sorry. I did not pay attention to the first sentence of yours. Yes for this specific case there shouldn't be a problem :) – Abhirath Mahipal Sep 11 '16 at 06:51
  • What's your take on converting 1 followed by 1000 zeroes (i.e binary), storing them in a char array and converting them to decimal? – Abhirath Mahipal Sep 11 '16 at 06:56
  • 1
    @AbhirathMahipal: the problem is that to convert a binary number to decimal you need to implement modulo and division. Simpler is instead to use base-10000 numbers and only implement addition and multiplication. – 6502 Sep 11 '16 at 07:03
  • See the edit @6502 – Prashant Sep 11 '16 at 07:24
  • @Prashant: Se edited answer. For such a big power you will need to use power by squares algorithm (each multiplication is - in the easy implementation - `O(n^3)` in the number of digits, and you won't to have the patience to wait for 10,000,000 of them :-) ). – 6502 Sep 12 '16 at 06:21
3

Store the digits in an int array where each location of the array denotes one digit. Then multiply them repetitively. That way you will get the answer with out crashing the compiler.

Well you need 302 locations for that. And the multiplication is simply the one that we do in grade classes. You have implement it in coding.

Little bit of code

int d[400];

for(int i=0;i<399;i++)
   d[i]=0;

d[0]=1;

int carry=0;
int temp=0;
for(int j=0;j<=999;j++)
{
   carry=0;
   temp=0;
   for(int i=0;i<=399;i++)
   {
      temp=d[i]*2+carry;
      d[i]= temp%10;
      carry = temp/10;
   }
}
print d[0..399] in reverse order trimming zeroes.
Community
  • 1
  • 1
user2736738
  • 30,591
  • 5
  • 42
  • 56
1

Unlike Python/Java, C++ does not handle such big number by itself nor does it have a dedicated data type for it. You need to use an array to store the numbers. You do not have a data type for the problem. These kind of questions are common in competitive programming sites. Here is a detailed tutorial.

Large Number in C/C++

You can also learn about bit manipulation. They are handy when you multiply by 2.

Please read this before using pow(2., 1000) as mentioned in another answer. c++ pow(2,1000) is normaly to big for double, but it's working. why?

As @6502 cleraly puts it in his answer, it can be used for this specific case of 2^1000. I missed that, be careful about that in case you are going to use this in a competitive programming site.

Community
  • 1
  • 1
Abhirath Mahipal
  • 938
  • 1
  • 10
  • 21
  • Bit manipulation not working…! I dont know why?? – Prashant Sep 11 '16 at 06:35
  • A long approach that comes to mind when using bit manipulation (You get to learn even though this approach is mundane). 2 ^ 2000 is just 1 followed by 2000 zeroes. You don't even have to calculate it. You can store in an array. Write a program that converts binary to decimal and you're good to go :) – Abhirath Mahipal Sep 11 '16 at 06:38
  • However if you want to display it in binary, you don't even have to convert it to decimal. – Abhirath Mahipal Sep 11 '16 at 06:39