Commit 0f004909 by Andrew Dahl

Solved Problem 30

parent 408ec9ba
Showing with 47 additions and 0 deletions
Question:
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
1634 = 1^4 + 6^4 + 3^4 + 4^4
8208 = 8^4 + 2^4 + 0^4 + 8^4
9474 = 9^4 + 4^4 + 7^4 + 4^4
As 1 = 1^4 is not a sum it is not included.
The sum of these numbers is 1634 + 8208 + 9474 = 19316.
Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.
Answer:
443839
#include <iostream>
#include <sstream>
#include <string>
#include <math.h>
using namespace std;
const int POW = 5;
const int MAX = 10000000;
int main() {
stringstream oss;
string str = "";
int tmp = 0;
int sum = 0;
for(int i = 2; i < MAX; i++) {
oss << i;
str = oss.str();
tmp = 0;
for(int j = 0; j < str.length(); j++) {
tmp += pow(str[j]-48,POW);
}
if(i == tmp)
sum += tmp;
str = "";
oss.str( string() );
oss.clear();
}
cout << sum << endl;
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment