-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.js
73 lines (66 loc) · 1.96 KB
/
5.js
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
JG.solution(5, () => {
/* START
Smallest multiple
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
END */
const maxFactor = 20
// we need to find the smallest set of primes which *every* factor can be made from
// ie the power set (?)
// then multiply them together
const powerSetPrimes = {} // { 2: 3, 7: 1 } for 2 * 2 * 2 * 7
for (let i = 2; i <= maxFactor; i++) {
// for each factor, find its full prime factorisation
const primes = allPrimeFactors(i)
for (let prime in primes) {
// add them all to the power set
if (!powerSetPrimes[prime] || powerSetPrimes[prime] < primes[prime]) {
powerSetPrimes[prime] = primes[prime]
}
}
}
// multiply the power set of primes
let product = 1
for (let prime in powerSetPrimes) {
product *= Math.pow(prime, powerSetPrimes[prime])
}
// inc all factors, even duplicates - 100 -> [2, 2, 5, 5] (not just [2, 5])
function allPrimeFactors (num) {
const primes = findPrimes(num)
const all = {}
let remaining = num
for (let prime of primes) {
let repeat = true
while (repeat) {
if (remaining % prime === 0) {
remaining /= prime
if (!all[prime]) all[prime] = 1
else all[prime]++
} else {
repeat = false
}
}
}
return all
}
function findPrimes (num) {
const factors = findFactors(num)
const primes = []
for (let factor of factors) {
if (isPrime(factor)) primes.push(factor)
}
return primes
}
function findFactors (num) {
if (num % 1 !== 0) return [] // must be an int
const factors = []
for (let i = num; i > 0; i--) {
if (num % i === 0) factors.push(i)
}
return factors
}
function isPrime (num) {
return findFactors(num).length === 2
}
return product
})