1

I tried to solve problem set 16 in projecteuler.net. I am trying to find 2^1000 for which I had written a code in C with a function named power().The problem here is that if i place printf() inside power() to get the result value, the answer is 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376.000000 but if I use return value in power() and try to print the result from main() the answer is -2147483648.000000. here is my code.

#include <stdio.h>

int power(double base,int ex)
{
    int i = 1;
    double final = 0;
    double ans = 1;
    while (i <= ex){
        ans *= base;
        i++;
    }
    printf("%lf\n",ans);     // return ans;
}


int main()
{
    double num;
    double result;
    int power_value;
    printf("enter the base value\n");
    scanf("%lf",&num);
    printf("enter the exponent value\n");
    scanf("%d",&power_value);
    power(num,power_value); //result = power(num,power_value)
    //printf("%lf",result);
}
Vishaal Shankar
  • 1,648
  • 14
  • 26
  • c is not c++ and `double main()` is certainly not c++ – 463035818_is_not_an_ai Mar 19 '18 at 10:08
  • C and C++ are two very different languages. If you program in C and want a solution in C then use only that tag. – Some programmer dude Mar 19 '18 at 10:08
  • 4
    As for your problem, you should do more research about [the standard types and their ranges](http://en.cppreference.com/w/c/language/arithmetic_types). And try to find a *bignum* library if you want anything outside the possibilities of the standard types. – Some programmer dude Mar 19 '18 at 10:10
  • 3
    The idea of this projecteuler question is to find a way to calculate the sum of the digits of `2^1000` without calculating `2^1000`, since (as all the others mentioned) it does not fit into a common datatype. – schorsch312 Mar 19 '18 at 13:13

2 Answers2

3

The reason that a different gets printed inside power as opposed to main is because the return type of power is int. As a result, the value gets truncated to what will fit in an int.

Change the return type to double so that it matches the type of the variables you're using and you'll get the expected result.

double power(double base,int ex)

As others have mentioned, numbers this large are outside the range of an int, and most numbers this size can't be accurately represented in a double. In this particular case however, because the number you're calculating is a power of two it can be represented exactly. Had you chosen a base besides 2 (or a power of 2), the result would not be exact.

dbush
  • 205,898
  • 23
  • 218
  • 273
2

The number you want to calculate is actually much greater than standard types in C can handle. long on todays machines has almost always a width of 64 bits, so the maximum number that can be stored in it is something like 2^64 = 18.446.744.073.709.551.615 < 2^2000

Also you can't start a program with double main() { }. Specification requires you to use int or void.

You'll have to find some library that does that calculation for you.

ulmer-a
  • 387
  • 1
  • 11