Project Euler - problem 030
August 1st, 2007 by Daniel
Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:
1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44
As 1 = 14 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #include <iostream> using namespace std; #define ANT 7 #define opp 5 int opphoyd(int num1, int num2){ int res =1; for(int i = 0 ; i < num2; i++){ res *= num1; } return res; } bool number (int arg[], int length) { int num1 = 0; for (int n=0; n<length; n++){ num1 += opphoyd(10, length-n-1) * arg[n]; } int num2= 0; for (int n=0; n<length; n++){ num2 += opphoyd(arg[n], opp); } return num1 == num2; } int main () { int res = 0; int temp= 0; int tab[ANT]; int pos = (ANT-1); while(true){ tab[pos]++; while(tab[pos]>9){ tab[pos]=0; pos--; tab[pos]++; } if(pos < 0){ break; } if(number(tab, ANT)){ temp = 0; for (int n=0; n<ANT; n++){ temp += opphoyd(tab[n], opp); } res += temp; } pos= ANT - 1; // cout << res ; // cout << " n"; } res --; cout << res ; cout << " n"; return 0; } |
What’s weird in this question is that no upper limit has been mentioned? Or does this kinda narcissity not occur over a billion digits at all?
Harsh: This occur not over 6 digits, but you need to prove that mathematical or test it.