-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem30.html
251 lines (232 loc) · 6.85 KB
/
Problem30.html
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<html>
<head>
<title>
Project Euler, Problem 30: Digit Fifth Powers
</title>
<link rel="stylesheet" type="text/css" href="eulerStyle.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<header>
<svg height="5rem" width="90vw">
<a href="https://dkallen78.github.io/Project-Euler-Files/projectEulerIndex.html">
<text id="euler" x="50%" y="50%" text-anchor="middle" dominant-baseline="middle">
Project Euler
</text>
</a>
</svg>
<br />
<svg height="4rem" width="90vw">
<a href="https://projecteuler.net/problem=30">
<text id="problemID" x="50%" y="50%" text-anchor="middle" dominant-baseline="middle">
Problem 30: Digit Fifth Powers
</text>
</a>
</svg>
</header>
<nav>
<svg height="3rem" width="75vw">
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem29.1.html">
<text x="0%" y="50%" text-anchor="start" dominant-baseline="middle">
Problem 29 (Binary Insert)
</text>
</a>
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem31.html">
<text x="100%" y="50%" text-anchor="end" dominant-baseline="middle">
Problem 31
</text>
</a>
</svg>
</nav>
<main>
<p>
Surprisingly there are only three numbers that can be written as the sum of
fourth powers of their digits:
<br />
<div class="inset">
<math>
<mn> 1634 </mn>
<mo> = </mo>
<msup>
<mn> 1 </mn>
<mn> 4 </mn>
</msup>
<mo> + </mo>
<msup>
<mn> 6 </mn>
<mn> 4 </mn>
</msup>
<mo> + </mo>
<msup>
<mn> 3 </mn>
<mn> 4 </mn>
</msup>
<mo> + </mo>
<msup>
<mn> 4 </mn>
<mn> 4 </mn>
</msup>
</math><br />
<math>
<mn> 8208 </mn>
<mo> = </mo>
<msup>
<mn> 8 </mn>
<mn> 4 </mn>
</msup>
<mo> + </mo>
<msup>
<mn> 2 </mn>
<mn> 4 </mn>
</msup>
<mo> + </mo>
<msup>
<mn> 0 </mn>
<mn> 4 </mn>
</msup>
<mo> + </mo>
<msup>
<mn> 8 </mn>
<mn> 4 </mn>
</msup>
</math><br />
<math>
<mn> 9474 </mn>
<mo> = </mo>
<msup>
<mn> 9 </mn>
<mn> 4 </mn>
</msup>
<mo> + </mo>
<msup>
<mn> 4 </mn>
<mn> 4 </mn>
</msup>
<mo> + </mo>
<msup>
<mn> 7 </mn>
<mn> 4 </mn>
</msup>
<mo> + </mo>
<msup>
<mn> 4 </mn>
<mn> 4 </mn>
</msup>
</math><br />
</div><br />
As
<math>
<mn> 1 </mn>
<mo> = </mo>
<msup>
<mn> 1 </mn>
<mn> 4 </mn>
</msup>
</math> is not a sum it is not included.
<br /><br />
The sum of these numbers is
<math>
<mn> 1634 </mn>
<mo> + </mo>
<mn> 8208 </mn>
<mo> + </mo>
<mn> 9474 </mn>
<mo> = </mo>
<mn> 19316 </mn>
</math>.
<br /><br />
Find the sum of all the numbers that can be written as the sum of fifth powers
of their digits.
</p>
<button id="problem" onclick="projectEulerProblem30()">
Find Sums
</button>
<br />
<summary id="notes">
<div id="totalTime"></div>
<p>
The tricky part for me was finding an upper limit to my search but I realized
that any 7-digit number would be out of bounds. I also optimized how I incremented
through numbers. Unfortunately I am not good enough at math to explain it... ☹
<br /><br />
Once I had all of that put together, I used a while loop and a bunch of if statements
to run through the program.
<br /><br />
108,205
</p>
</summary>
</main>
</body>
<script>
function projectEulerProblem30() {
//108205
let startTime = new Date();
let coolNumbers = [];
let run = true;
let n = 100
while (run === true) {
//
//finds the sum of the number
let sum = sumPowers(n, 5);
//
//The upper limit for my search
if (n > 1000000) {
run = false;
}
//
//If the sum is smaller than n, increment by 1
if (sum < n) {
n++
} else if (sum > n) {
//
//If the sum is greater than n, go to the
//next 10 or 100
if ((n % 10) > 0) {
n += (10 - (n % 10));
} else {
n += (100 - (n % 100));
}
//
//If the sum and n match, add it to the list
} else {
coolNumbers.push(n);
n++;
}
}
let sums = 0;
for (let i = 0; i < coolNumbers.length; i++) {
sums += coolNumbers[i];
}
let endTime = new Date();
let totalTime = endTime - startTime;
document.getElementById("totalTime").innerHTML = totalTime + " ms";
document.getElementById("notes").style.display = "block";
document.getElementById("problem").innerHTML = sums;
}
function sumPowers(number, power) {
//----------------------------------------------------//
//Finds the sums of the powers of the digits //
// of a number //
//integer-> number: the number //
//integer-> power: the power to raise the digits to //
//----------------------------------------------------//
let digits = [];
//
//Breaks the number down into its
//constituent digits
while (number > 9) {
digits.push(number % 10);
number = (number - (number % 10)) / 10;
}
digits.push(number);
//
//Finds the sum of the powers
let sum = 0;
for (let i = 0; i < digits.length; i++) {
sum += digits[i] ** power;
}
return sum;
}
</script>
</html>