Power digit sum | Project Euler | Problem #16
URL to the problem page: https://projecteuler.net/problem=16
2¹⁵ = 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¹⁰⁰⁰?
2¹⁵ = 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¹⁰⁰⁰?
#include <iostream>
using namespace std;
int main()
{
const int k = 504;
int number[k] = { 0 }, result[k] = { 0 }, carry, sum = 0, i, a;
number[k - 1] = 2;
for (a = 1; a < 1000; a++) {
carry = 0;
for (i = k - 1; i >= 0; i--) {
result[i] = ((2 * number[i]) + carry) % 10;
carry = ((2 * number[i]) + carry) / 10;
}
for (i = k - 1; i >= 0; i--) {
number[i] = result[i];
}
}
for (i = 0; i < k; i++) {
sum += number[i];
}
cout << "Sum of the digits of the number 2^1000 is = " << sum << endl;
Comments
Post a Comment