-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler30.html
210 lines (188 loc) · 5.46 KB
/
euler30.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
<html>
<head>
<title>Project Euler, Problem 30: Digit Fifth Powers</title>
<style>
body {
background-image: linear-gradient(to right, blue , white);
}
#header {
font-size: 2em;
font-weight: bold;
text-align: center;
color: white;
text-shadow: 2px 0px black,
0px 2px black,
-2px 0px black,
0px -2px black;
}
#problemNumber {
font-size: 1.5em;
text-align: center;
color: white;
text-shadow: 2px 0px black,
0px 2px black,
-2px 0px black,
0px -2px black;
}
#links {
margin: auto;
margin-bottom: 10px;
font-size: 1.25em;
text-align: center;
color: white;
text-shadow: 2px 0px black,
0px 2px black,
-2px 0px black,
0px -2px black;
}
a:visited, a:link {
color: white;
text-decoration: none;
}
#workArea {
margin: auto;
width: 50vw;
border: 3px solid black;
border-radius: 5px;
padding: 5px;
background-color: white;
box-shadow: -10px 10px 10px;
}
.example {
text-align: center;
}
.inset {
margin-left: 1em;
}
#explanation {
visibility: hidden;
}
#problem {
text-align: center;
border: 2px solid black;
border-radius: 5px;
width: 105px;
cursor: default;
}
</style>
</head>
<body>
<p id="header"><a href="https://dkallen78.github.io/Project-Euler-Files/projectEulerIndex.html">Project Euler</a></p>
<!--<p id="problemNumber">
<a href="https://projecteuler.net/problem=10">Problem 10: Sumation of Primes</a><br />
(Prime Seive)
</p>
<div id="links">
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem10.html">Problem 10 (Primality Test)</a> -
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem11.html">Problem 11</a>
</div>-->
<div id="workArea">
<p>
Surprisingly there are only three numbers that can be written as the sum of
fourth powers of their digits:
<br />
<div class="inset">
1634 = 1⁴ + 6⁴ + 3⁴ + 4⁴<br />
8208 = 8⁴ + 2⁴ + 0⁴ + 8⁴<br />
9474 = 9⁴ + 4⁴ + 7⁴ + 4⁴<br />
</div><br />
As 1 = 1⁴ is not a sum it is not included.
<br /><br />
The sum of these numbers is 1634 + 8208 + 9474 = 19316.
<br /><br />
Find the sum of all the numbers that can be written as the sum of fifth powers
of their digits.
</p>
<div id="problem" onclick="projectEulerProblem30()">
Find Sums
</div>
<div id="explanation">
<br />
<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>
</div>
</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];
}
//console.log(sums);
let endTime = new Date();
let totalTime = endTime - startTime;
document.getElementById("totalTime").innerHTML = totalTime + " ms";
document.getElementById("explanation").style.visibility = "visible";
document.getElementById("problem").innerHTML = sums;
//let sum = sumPowers(20, 5);
//console.log(typeof sum);
}
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>