Digit Fifth Powers
C++, Matlab. This is the Problem 30 from Project Euler.
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. Link to the page.
Solution: Firstly, I found the maximum number based on input digit number for next counting loop. The next algorithm checks the matches with digit powered numbers. If found, it takes it into the dynamic array. Then, it sums up, and shows the result.
For n = 5 (fifth power), the result is 443839.
The numbers: 4150, 4151, 54748, 92727, 93084, and 194979.
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
#include <iostream> #include <cmath> #include <vector> void digitpower(int n); int main() { int n = 5; // input power degree value digitpower(n); return 0; } void digitpower(int n) { if (n <= 1) { std::cout << "Input power value is invalid" << std::endl; return; } /*FINDING MAXIMUM NUMBER STAGE*/ //Maximum number is the limit for counting //i represents number of integer digits in the loop below int max_number = 0; int digit_number = 0; for (int i = 1; i <= n; ++i) { max_number = i * static_cast<int>(pow(9, n)); digit_number = static_cast<int>(floor(log10(max_number))) + 1; if (i == digit_number) { digit_number = digit_number; max_number = max_number; break; } } /*SEARCHING STAGE*/ //Number 1 is not included for counting std::vector<int> list; //dynamic array for (int j = 2; j <= max_number; ++j) { int x = 0; int digit_value = 0; //numerical value for each digit int total_value = 0; //sum of numerical values of powered digits x = static_cast<int>(floor(log10(j))) + 1; //number of digits for the counting integer for (int k = x; k >= 1; --k) { digit_value = static_cast<int>(fmod(floor(j / pow(10, k - 1)), 10)); total_value += static_cast<int>(pow(digit_value, n)); } if (j == total_value) { list.push_back(j); //Creating list for numbers } } //For invalid inputs, and when no such a number is found in its range if (list.size() <= 0) { std::cout << "There is no such a number for this input" << std::endl; return; } /*FINAL STAGE*/ //The sum of numbers in the list, and the numbers int sum = 0; int size = list.size(); //size of array, to avoid compiler mismatch error for (int i = 0; i < size; ++i) { sum += list[i]; } /*OUTPUT STAGE*/ std::cout << "The sum of digit powered numbers: " << sum << std::endl; std::cout << "The numbers: "; for (int j = 0; j < size - 1; ++j) { std::cout << list[j] << ", "; } std::cout << list.back() << std::endl; } |
MATLAB version;
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 |
function result = digit_fifth_powers tic n = 5; %Power degree input %FINDING MAXIMUM NUMBER STAGE %Maximum number is the limit for counting %i represents number of integer digits in the loop below max_number = 0; for i = 1:100 max_number = i * (9^n); digit_number = floor(log10(max_number)) + 1; if i == digit_number digit_number = digit_number; max_number = max_number; break end end %SEARCHING STAGE % Number 1 is not included for counting list = []; for j = 2:max_number x = floor(log10(j)) + 1; digits = mod(floor(j ./ 10 .^ ((x+1):-1:0)), 10); sum_digits = sum(digits .^ n); if j == sum_digits list = [list, j]; %Creating list for numbers meets the condition end end %FINAL STAGE %The first column is the sum of numbers in the list result = [sum(list), list]; toc end |