-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob30.cpp
70 lines (58 loc) · 1.64 KB
/
prob30.cpp
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>
#include <cmath>
#include <deque>
#include <vector>
using namespace std;
int main()
{
// Perform some pre-calculations
vector<int> pows;
for (int i = 0; i <= 9; ++i) {
pows.push_back(pow(i, 5));
}
vector<int> ten_pows;
for (int i = 0; i <= 6; ++i) {
ten_pows.push_back(pow(10, i));
}
deque<int> num;
num.push_back(2);
int num_digits = 1;
bool advance;
int num_val, pow_val;
int inc_indx = 0;
int sum = 0;
while (num_val < 354294) {
num_val = 0;
pow_val = 0;
// Calculate the true & power value
for (int i = 0; i < num_digits; ++i) {
pow_val += pows[num[i]];
num_val += num[i] * ten_pows[num_digits - i - 1];
}
advance = false;
if (num_val == pow_val) {
sum += num_val;
} else if (pow_val > num_val) {
advance = true;
}
if (num[inc_indx] == 9 || advance) {
// go to an earlier col not at value 9
while (--inc_indx >= 0) {
if (num[inc_indx] < 9) break;
}
if (inc_indx < 0) { // add a new column to the front
num.push_front(1);
num_digits++;
for (int i = 1; i < num_digits; ++i) num[i] = 0;
} else {
++num[inc_indx];
for (int i = inc_indx + 1; i < num_digits; ++i) num[i] = 0;
}
inc_indx = num_digits - 1;
} else {
++num[inc_indx];
}
}
cout << sum << endl;
return 0;
}