The problem
I am trying to solve the following:
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 keep getting false outputs because of precision errors. However, as you can see below, I try to make sure that nothing is rounded, or lost because of precision. The code I wrote falls short of the desired output (1366) by two (1364). How can optimize my code in order to not lose anything else because of precision and rounding errors?
My code
#include <iostream>
#include <tgmath.h>
#include <string>
using namespace std;
int main() {
int sum=0;
double N = pow(2.0, 1000.0);
string num = to_string(N);
for(int i=0; i<num.size(); i++) {
sum += num[i] - '0';
}
cout<<sum;
return 0;
}