-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliquot2.js
34 lines (29 loc) · 729 Bytes
/
aliquot2.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
const divisors = (integer) => {
const result = []
for(let i = 1; i < integer; i++) {
if(integer % i == 0) result.push(i)
}
return result
}
const sum = x => x.reduce(
(partialSum, i) => partialSum + i, 0
)
const s = (integer) => sum(divisors(integer))
const aliquot = (integer) => {
let steps = 0
const previous = []
let value = integer
console.log(steps, value)
let lastTime = new Date().getTime()
let currentTime = new Date().getTime()
while (true) {
steps += 1
previous.push(value)
value = s(value)
currentTime = new Date().getTime()
console.log(steps, value, currentTime-lastTime, 'ms')
lastTime = currentTime
if (previous.includes(value)) break
}
}
aliquot(276)