-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem8.html
243 lines (222 loc) · 9.87 KB
/
Problem8.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
<html>
<head>
<title>
Project Euler, Problem 8: Largest product in a series
</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=8">
<text id="problemID" x="50%" y="50%" text-anchor="middle" dominant-baseline="middle">
Problem 8: Largest Product in a Series
</text>
</a>
</svg>
</header>
<nav>
<svg height="3rem" width="75vw">
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem7.html">
<text x="0%" y="50%" text-anchor="start" dominant-baseline="middle">
Problem 7
</text>
</a>
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem9.html">
<text x="100%" y="50%" text-anchor="end" dominant-baseline="middle">
Problem 9
</text>
</a>
</svg>
</nav>
<main>
<p>
The four adjacent digits in the 1000-digit number that have the greatest
product are 9 × 9 × 8 × 9 = 5832.<br />
<div class="example">
73167176531330624919225119674426574742355349194934<br />
96983520312774506326239578318016984801869478851843<br />
12540698747158523863050715693290963295227443043557<br />
85861560789112949495459501737958331952853208805511<br />
66896648950445244523161731856403098711121722383113<br />
62229893423380308135336276614282806444486645238749<br />
30358907296290491560440772390713810515859307960866<br />
70172427121883998797908792274921901699720888093776<br />
65727333001053367881220235421809751254540594752243<br />
52584907711670556013604839586446706324415722155397<br />
53697817977846174064955149290862569321978468622482<br />
83972241375657056057490261407972968652414535100474<br />
82166370484403199890008895243450658541227588666881<br />
16427171479924442928230863465674813919123162824586<br />
17866458359124566529476545682848912883142607690042<br />
24219022671055626321111109370544217506941658960408<br />
07198403850962455444362981230987879927244284909188<br />
84580156166097919133875499200524063689912560717606<br />
05886116467109405077541002256983155200055935729725<br />
71636269561882670428252483600823257530420752963450<br />
</div>
<br /><br />
Find the thirteen adjacent digits in the 1000-digit number that have the
greatest product. What is the value of this product?
</p>
<button id="problem" onclick="projectEulerProblem8()">
Find Product
</button>
<br />
<summary id="notes">
<div>Runtime: <span id="totalTime">0</span></div>
<div>Average: <span id="avgTime">0</span> Runs: <span id="runs">0</span></div>
<div>SD: <span id="stdDev">0</span> ms</div>
<div class="red">Max: <span id="max">0</span></div>
<div class="green">Min: <span id="min">1000</span></div>
<p>
I store the large number in a string and work on it 13 digits at a time.
</br></br>
If I encounter a 0 in the string, I don't bother multiplying it because I know the product will be 0.
I save the highest product as I go along.
</p>
</summary>
</main>
</body>
<script>
let population = [];
function projectEulerProblem8() {
function display(totalTime, solution) {
//----------------------------------------------------//
//Displays the information to the window after the //
// program has run //
//float-> totalTime: time it took to run the program //
//----------------------------------------------------//
function getAvg(totalTime) {
//----------------------------------------------------//
//Calculates the average time to run the program //
//float-> totalTime: time it took to run the program //
//----------------------------------------------------//
let oldAvg = parseFloat(document.getElementById("avgTime").innerHTML, 10);
let oldIter = Number(document.getElementById("runs").innerHTML);
let newIter = oldIter + 1;
document.getElementById("runs").innerHTML = newIter;
let avgTime = ((oldAvg * oldIter) + totalTime) / newIter;
return avgTime.toFixed(3);
}
function getSD(average, population) {
//----------------------------------------------------//
//Calculates standard deviation of the runtimes //
//float-> average: average program runtime //
//array-> population: an array of runtimes //
//----------------------------------------------------//
average = Number(average);
let sum = 0;
for (let i = 0; i < population.length; i++) {
sum += (population[i] - average) ** 2;
}
sum /= population.length;
return Math.sqrt(sum).toFixed(3);;
}
population.push(totalTime);
//
//Display the solution
document.getElementById("problem").innerHTML = solution;
//
//Display the runtime
document.getElementById("totalTime").innerHTML = totalTime.toFixed(3) + " ms";
let average = getAvg(totalTime);
//
//Display the average
document.getElementById("avgTime").innerHTML = average + " ms";
//
//Display the standard deviation
document.getElementById("stdDev").innerHTML = getSD(average, population);
//
//Display the highest runtime
let max = parseFloat(document.getElementById("max").innerHTML, 10);
if (totalTime > max) {
document.getElementById("max").innerHTML = totalTime.toFixed(3) + " ms";
}
//
//Display the lowest runtime
let min = parseFloat(document.getElementById("min").innerHTML, 10);
if (totalTime < min) {
let minString = "";
if (totalTime === 0) {
minString = "< 0.020";
} else {
minString = totalTime.toFixed(3);
}
document.getElementById("min").innerHTML = minString + " ms";
}
//
//Display the explanatory notes
document.getElementById("notes").style.display = "block";
let summary = document.querySelector("summary").scrollHeight;
setTimeout(function() {
document.querySelector("summary").style.height = summary + "px";
}, 1);
}
const startTime = performance.now();
let bigNumber = "73167176531330624919225119674426574742355349194934" +
"96983520312774506326239578318016984801869478851843" +
"85861560789112949495459501737958331952853208805511" +
"12540698747158523863050715693290963295227443043557" +
"66896648950445244523161731856403098711121722383113" +
"62229893423380308135336276614282806444486645238749" +
"30358907296290491560440772390713810515859307960866" +
"70172427121883998797908792274921901699720888093776" +
"65727333001053367881220235421809751254540594752243" +
"52584907711670556013604839586446706324415722155397" +
"53697817977846174064955149290862569321978468622482" +
"83972241375657056057490261407972968652414535100474" +
"82166370484403199890008895243450658541227588666881" +
"16427171479924442928230863465674813919123162824586" +
"17866458359124566529476545682848912883142607690042" +
"24219022671055626321111109370544217506941658960408" +
"07198403850962455444362981230987879927244284909188" +
"84580156166097919133875499200524063689912560717606" +
"05886116467109405077541002256983155200055935729725" +
"71636269561882670428252483600823257530420752963450";
let index = 0;
let highString = "";
let highStringProduct = 0;
let finalProduct = 1;
do {
let testString = "";
testString = bigNumber.substr(index, 13);
//
//If the number we're checking includes a 0
//then go on to the next loop
if (testString.includes("0")) {
index++;
continue;
}
//
//Works out the product of the 13 digits
let productHolder = 1;
for (let x = 0; x < 13; x++) {
productHolder *= parseInt(testString[x], 10);
}
//
//Stores the product if it's higher than the
//previous one
if (productHolder > highStringProduct) {
highString = testString;
highStringProduct = productHolder;
}
index++;
}
while (index < (bigNumber.length - 13));
const endTime = performance.now();
const totalTime = parseFloat((endTime - startTime).toFixed(3), 10);
display(totalTime, highStringProduct);
}
</script>
</html>