-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproblem0005.js
More file actions
79 lines (62 loc) · 1.98 KB
/
problem0005.js
File metadata and controls
79 lines (62 loc) · 1.98 KB
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
/**
* @link Problem definition [[docs/projecteuler/problem0005.md]]
*/
/// ////////////////////////////////////////////////////////////////////////////
// Using prime factors approach.
//
// FOUND: 232792560 divisible by any element beetwen 1 and 20
/// ////////////////////////////////////////////////////////////////////////////
import { logger as console } from '../logger.js';
import { primeFactors } from './helpers/divisors.js';
function _increase(_element, _group) {
const group = _group;
if (Object.hasOwn(group, _element)) {
group[_element] += 1;
} else {
group[_element] = 1;
}
return _group;
}
function _replaceMaximum(_element, count, _group) {
const group = _group;
if (Object.hasOwn(group, _element)) {
const elem = _group[_element];
group[_element] = Math.max(count, elem);
} else {
group[_element] = count;
}
return _group;
}
function _primeFactorsCollection(_factors) {
let collection = {};
for (const factor of _factors) {
collection = _increase(factor, collection);
}
return collection;
}
function problem0005(_bottom, _top) {
const minimumPrimeFactors = {};
let result = 1;
let cycles = 0;
for (let i = _bottom; i <= _top; i++) {
const primes = primeFactors(i);
cycles += primes.cycles;
const factors = _primeFactorsCollection(primes.factors);
cycles += primes.factors.length;
console.debug('Prime Factors of %d list => %o', i, primes);
console.debug('Prime Factors of %d grouped => %o', i, factors);
for (const [factor, quantity] of Object.entries(factors)) {
cycles += 1;
_replaceMaximum(factor, quantity, minimumPrimeFactors);
}
console.debug('Prime Factors of %d grouped => %o', i, minimumPrimeFactors);
}
for (const [factor, quantity] of Object.entries(minimumPrimeFactors)) {
cycles += 1;
result *= factor ** quantity;
}
console.log('Problem 0005: %d in %d cycles', result, cycles);
return result;
}
export default problem0005;
export { problem0005 };