-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem21.html
341 lines (304 loc) · 9.74 KB
/
Problem21.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
<html>
<head>
<title>
Project Euler, Problem 21: Amicable Numbers
</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=21">
<text id="problemID" x="50%" y="50%" text-anchor="middle" dominant-baseline="middle">
Problem 21: Amicable Numbers
</text>
</a>
</svg>
</header>
<nav>
<svg height="3rem" width="75vw">
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem20.html">
<text x="0%" y="50%" text-anchor="start" dominant-baseline="middle">
Problem 20
</text>
</a>
<a href="https://dkallen78.github.io/Project-Euler-Files/Problem22.html">
<text x="100%" y="50%" text-anchor="end" dominant-baseline="middle">
Problem 22
</text>
</a>
</svg>
</nav>
<main>
<p>
<div>
Let
<math>
<mi>d</mi>
<mo>⁡</mo>
<mo>(</mo>
<mi>n</mi>
<mo>)</mo>
</math>
be defined as the sum of proper divisors of
<math>
<mi>n</mi>
</math>
(numbers less than
<math>
<mi>n</mi>
</math>
which divide evenly into
<math>
<mi>n</mi>
</math>).
If
<math>
<mi>d</mi>
<mo>⁡</mo>
<mo>(</mo>
<mi>a</mi>
<mo>)</mo>
<mo>=</mo>
<mi>b</mi>
</math>
and
<math>
<mi>d</mi>
<mo>⁡</mo>
<mo>(</mo>
<mi>b</mi>
<mo>)</mo>
<mo>=</mo>
<mi>a</mi>
</math>,
where
<math>
<mi>a</mi>
<mo>≠</mo>
<mi>b</mi>
</math>,
then
<math>
<mi>a</mi>
</math>
and
<math>
<mi>b</mi>
</math>
are an amicable pair and each of
<math>
<mi>a</mi>
</math>
and
<math>
<mi>b</mi>
</math>
are called amicable numbers.
</div>
<br />
<div>
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44,
55 and 110; therefore
<math>
<mi>d</mi>
<mo>⁡</mo>
<mo>(</mo>
<mn>220</mn>
<mo>)</mo>
<mo>=</mo>
<mn>284</mn>
</math>.
The proper divisors of 284 are 1, 2,
4, 71 and 142; so
<math>
<mi>d</mi>
<mo>⁡</mo>
<mo>(</mo>
<mn>284</mn>
<mo>)</mo>
<mo>=</mo>
<mn>220</mn>
</math>.
</div>
<br />
Evaluate the sum of all the amicable numbers under 10000.
</p>
<button id="problem" onclick="projectEulerProblem21()">
Find Sum
</button>
<summary id="notes">
<div id="totalTime"></div>
<p>
The two non-trivial parts of this problem involve getting the prime factors
of a number and, based on that, finding all factors of that number.
<br /><br />
To find the prime factors I just divide out progressively higher prime numbers
using loops. To find all factors I multiply the prime factors with each other.
<br /><br />
Once I have these two algorithms in place, I can iterate through the numbers,
looking for amicable pairs. When I find them, I add them to an array and
sum them.
<br /><br />
###,###
</p>
</summary>
</main>
</body>
<script>
function projectEulerProblem21() {
let startTime = new Date();
let testArray = [];
let test1;
let test2;
let amicableNumbers = [];
for (let x = 2; x < 10001; x++) {
//
//Prevents double checking of amicability
if (amicableNumbers.includes(x)) {
continue;
}
test1 = sumFactors(x);
test2 = sumFactors(test1);
if ((x === test2) && (test1 !== x)) {
amicableNumbers.push(x);
amicableNumbers.push(test1);
}
}
let friendlySum = 0;
//
//Sums the amicable numbers
for (let x = 0; x < amicableNumbers.length; x++) {
friendlySum += amicableNumbers[x];
}
let endTime = new Date();
let totalTime = endTime - startTime;
document.getElementById("problem").innerHTML = friendlySum;
document.getElementById("totalTime").innerHTML = totalTime + " ms";
document.getElementById("notes").style.display = "block";
}
function findPrimeFactors(number) {
//----------------------------------------------------//
//Finds the prime factors of a number //
//integer-> number: number to find factors from //
//----------------------------------------------------//
let factors = [];
//
//finds all the factors of 2
while ((number % 2) === 0) {
factors.push(2);
number /= 2;
}
let numberToTest = 3;
//
//Finds the odd numbered prime factors
while ((numberToTest * numberToTest) <= number) {
if ((number % numberToTest) === 0) {
factors.push(numberToTest);
number /= numberToTest;
} else {
numberToTest += 2;
}
}
if (number !== 1) {
factors.push(number);
}
return factors;
}
function getFactors(primeFactors) {
//----------------------------------------------------//
//Given an array of prime factors, finds all factors //
//array-> primeFactors: array of primes to derive //
// factors from //
//----------------------------------------------------//
let allFactors = [];
let product;
primeFactors.unshift(1);
//
//Multiplies pairs of numbers together going from
//right to left
for (let x = 0; x < (primeFactors.length - 1); x++) {
for (let y = (x + 1); y < primeFactors.length; y++) {
allFactors.push(primeFactors[x] * primeFactors[y]);
}
}
//
//Multiplies pairs of numbers together going from
//left to right
for (let x = (primeFactors.length - 1); x > 0; x--) {
for (let y = (x - 1); y >= 0; y--) {
allFactors.push(primeFactors[x] * primeFactors[y]);
}
}
//
//Multiplies the product of two numbers with
//another number going left to right
product = primeFactors[0];
for (let x = 1; x < (primeFactors.length - 1); x++) {
product *= primeFactors[x];
for (let y = (x + 1); y < primeFactors.length; y++) {
allFactors.push(product * primeFactors[y]);
}
}
//
//Multiplies the product of two numbers with
//another number going right to left
product = primeFactors[(primeFactors.length - 1)]
for (let x = (primeFactors.length - 2); x > 0; x--) {
product *= primeFactors[x];
for (let y = (x - 1); y > 0; y--) {
allFactors.push(product * primeFactors[y]);
}
}
//
//Cleans and sorts the final list of factors
allFactors = cleanFactors(allFactors);
allFactors.unshift(1);
allFactors.pop();
return allFactors;
function cleanFactors(factors) {
//----------------------------------------------------//
//Removes repeated numbers from an array //
//array-> factors: array of numbers //
//----------------------------------------------------//
let finalFactors = [];
//
//Goes through every element and removes repeats
for (let x = 0; x < factors.length; x++) {
if (!finalFactors.includes(factors[x])) {
finalFactors.push(factors[x]);
}
}
//
//Sorts the array, least to greatest
finalFactors.sort(function(a, b){return a-b});
return finalFactors;
}
}
function sumFactors(number) {
//----------------------------------------------------//
//Given a number, sums its factors //
//integer-> number: number to derive factors from //
// and to sum //
//----------------------------------------------------//
let sum = 0;
let factors = [];
factors = findPrimeFactors(number);
factors = getFactors(factors);
for (let x = 0; x < factors.length; x++) {
sum += factors[x];
}
return sum;
}
</script>
</html>