-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem29.1.html
381 lines (358 loc) · 10.9 KB
/
Problem29.1.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<html>
<head>
<title>
Project Euler, Problem 29: Distinct Powers (Binary Insert)
</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=29">
<text id="problemID" x="50%" y="50%" text-anchor="middle" dominant-baseline="middle">
Problem 29: Distinct Powers (Binary Insert)
</text>
</a>
</svg>
</header>
<nav>
<svg height="3rem" width="75vw">
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem29.html">
<text x="0%" y="50%" text-anchor="start" dominant-baseline="middle">
Problem 29
</text>
</a>
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem30.html">
<text x="100%" y="50%" text-anchor="end" dominant-baseline="middle">
Problem 30
</text>
</a>
</svg>
</nav>
<main>
<p>
Consider all integer combinations of
<math>
<msup>
<mi> a </mi>
<mi> b </mi>
</msup>
</math> for
<math>
<mn> 2 </mn>
<mo> ≤ </mo>
<mi> a </mi>
<mo> ≤ </mo>
<mn> 5 </mn>
</math> and
<math>
<mn> 2 </mn>
<mo> ≤ </mo>
<mi> b </mi>
<mo> ≤ </mo>
<mn> 5 </mn>
</math>:
<div class="inset">
<div>
<math>
<msup>
<mn> 2 </mn>
<mn> 2 </mn>
</msup>
<mo> = </mo>
<mn> 4 </mn>
</math>,
<math>
<msup>
<mn> 2 </mn>
<mn> 3 </mn>
</msup>
<mo> = </mo>
<mn> 8 </mn>
</math>,
<math>
<msup>
<mn> 2 </mn>
<mn> 4 </mn>
</msup>
<mo> = </mo>
<mn> 16 </mn>
</math>,
<math>
<msup>
<mn> 2 </mn>
<mn> 5 </mn>
</msup>
<mo> = </mo>
<mn> 32 </mn>
</math>
</div>
<div>
<math>
<msup>
<mn> 3 </mn>
<mn> 2 </mn>
</msup>
<mo> = </mo>
<mn> 9 </mn>
</math>,
<math>
<msup>
<mn> 3 </mn>
<mn> 3 </mn>
</msup>
<mo> = </mo>
<mn> 27 </mn>
</math>,
<math>
<msup>
<mn> 3 </mn>
<mn> 4 </mn>
</msup>
<mo> = </mo>
<mn> 81 </mn>
</math>,
<math>
<msup>
<mn> 3 </mn>
<mn> 5 </mn>
</msup>
<mo> = </mo>
<mn> 243 </mn>
</math>
</div>
<div>
<math>
<msup>
<mn> 4 </mn>
<mn> 2 </mn>
</msup>
<mo> = </mo>
<mn> 16 </mn>
</math>,
<math>
<msup>
<mn> 4 </mn>
<mn> 3 </mn>
</msup>
<mo> = </mo>
<mn> 64 </mn>
</math>,
<math>
<msup>
<mn> 4 </mn>
<mn> 4 </mn>
</msup>
<mo> = </mo>
<mn> 256 </mn>
</math>,
<math>
<msup>
<mn> 4 </mn>
<mn> 5 </mn>
</msup>
<mo> = </mo>
<mn> 1024 </mn>
</math>
</div>
<div>
<math>
<msup>
<mn> 5 </mn>
<mn> 2 </mn>
</msup>
<mo> = </mo>
<mn> 25 </mn>
</math>,
<math>
<msup>
<mn> 5 </mn>
<mn> 3 </mn>
</msup>
<mo> = </mo>
<mn> 125 </mn>
</math>,
<math>
<msup>
<mn> 5 </mn>
<mn> 4 </mn>
</msup>
<mo> = </mo>
<mn> 625 </mn>
</math>,
<math>
<msup>
<mn> 5 </mn>
<mn> 5 </mn>
</msup>
<mo> = </mo>
<mn> 3125 </mn>
</math>
</div>
</div>
<br />
If they are then placed in numerical order, with any repeats removed, we get
the following sequence of 15 distinct terms:
<br /><br />
<div class="example">
4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
</div>
<br />
How many distinct terms are in the sequence generated by
<math>
<msup>
<mi> a </mi>
<mi> b </mi>
</msup>
</math> for
<math>
<mn> 2 </mn>
<mo> ≤ </mo>
<mi> a </mi>
<mo> ≤ </mo>
<mn> 100 </mn>
</math> and
<math>
<mn> 2 </mn>
<mo> ≤ </mo>
<mi> b </mi>
<mo> ≤ </mo>
<mn> 100 </mn>
</math>?
</p>
<button id="problem" onclick="projectEulerProblem29()">
Find Terms
</button>
<br />
<summary id="notes">
<div id="totalTime"></div>
<p>
The tricky part of this problem was getting JavaScript to handle 𝔹𝕀𝔾<sup>®</sup>
numbers. Thankfully the BigInt is here and it made things a lot easier. I made
two helper functions: one to put my numbers in an array and another to trim extra
numbers after the fact.
<br /><br />
I run two nested for loops and compute every power, inserting the values as
I go.
<br /><br />
A major breakthrough on bringing the speed down for this problem was using a
binary search for inserting the numbers into the array. Another was trimming
the array of extra numbers instead of searching for duplicates before inserting.
Using a binary search before the fact only cost me about 1ms though.
<br /><br />
103,541
</p>
</summary>
</main>
</body>
<script>
function projectEulerProblem29() {
//103541
let startTime = new Date();
let powers = 100;
let distPowers = [];
//
//These nested loops run through every possible
//combination of bases and exponents
for (let i = 2n; i < powers + 1; i++) {
for (let j = 2n; j < powers + 1; j++) {
let power = i ** j;
binPush(power, distPowers);
}
}
//
//This removes the repeated terms
distPowers = trimArray(distPowers);
let endTime = new Date();
let totalTime = endTime - startTime;
document.getElementById("totalTime").innerHTML = totalTime + " ms";
document.getElementById("notes").style.display = "block";
document.getElementById("problem").innerHTML = distPowers.length;
console.log(distPowers);
}
function binSearch(number, array) {
//----------------------------------------------------//
//Checks to see if a BigInt is in an array of BigInts //
//integer-> number: number to search for //
//array-> array: array to search //
//----------------------------------------------------//
let run = true;
let low = 0;
let high = array.length - 1;
let index = Math.floor(high / 2);
while (run === true) {
if (array[index] === undefined) {
return false;
} else if (number === array[index]) {
return true;
} else if (number > array[index]) {
low = index + 1;
if (low > high) return false;
index += Math.ceil((high - index) / 2);
} else {
high = index - 1;
if (high < low) return false;
index -= Math.ceil((index - low) / 2);
}
}
}
function binPush(number, array) {
//----------------------------------------------------//
//Uses a binary search to insert numbers into //
// the array //
//integer-> number: number to be inserted //
//array-> array: array to be inserted into //
//----------------------------------------------------//
let run = true;
let low = 0;
let high = (array.length > 0) ? (array.length - 1) : 0;
let index = Math.floor(high / 2);
while (run === true) {
if (array[index] === undefined) {
array[index] = number;
return;
} else if (number === array[index]) {
array.splice(index + 1, 0, number);
return;
} else if (number > array[index]) {
low = index + 1;
if (low > high) {
array.splice(index + 1, 0, number);
return;
}
index += Math.ceil((high - index) / 2);
} else {
high = index - 1;
if (high < low) {
array.splice(index, 0, number);
return;
}
index -= Math.ceil((index - low) / 2);
}
}
}
function trimArray(array) {
//----------------------------------------------------//
//Removes duplicate elements from a sorted array //
//array-> array: sorted array with duplicate elements //
// to be removed //
//----------------------------------------------------//
for (let i = array.length - 1; i >= 0; i--) {
if (array[i] === array[i - 1]) {
array.splice(i, 1);
}
}
return array;
}
</script>
</html>