-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem14.js
48 lines (43 loc) · 1006 Bytes
/
problem14.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
cache = {}
function collatz(number) {
let chain = 1
const origNumber = number
while (number !== 1) {
if (number in cache) {
chain += cache[number]
break
}
if (number % 2 == 0) {
number = number / 2
}
else {
number = (number * 3) + 1
}
chain += 1
}
cache[origNumber] = chain
return chain
}
function problem14(limit=1000000) {
let longest = 0, temp, ans;
for (let number = 1; number < limit; number++) {
temp = collatz(number)
if (temp > longest) {
longest = temp
ans = number
}
}
return {longest, ans}
}
const startTime = Date.now()
const number = problem14()
const endTime = Date.now()
console.log('number: ', number)
const runningTime = endTime - startTime
console.log(`Running time (s): ${runningTime / 1000}`)
// stats before optimization
// number: { longest: 525, ans: 837799 }
// Running time (s): 1.336
// stats when optimized
// number: { longest: 556, ans: 837799 }
// Running time (s): 0.181