-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.ts
48 lines (44 loc) · 1.15 KB
/
14.ts
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
// Longest Collatz Sequence
function getNextNumber(n: number) {
if (n % 2 === 0) {
return n / 2;
}
return 3 * n + 1;
}
function getSequence(startingNumber: number) {
const sequence = [startingNumber];
let lastNumber = startingNumber;
while (true) {
const nextNumber = getNextNumber(lastNumber);
sequence.push(nextNumber);
if (nextNumber === 1) {
break;
}
lastNumber = nextNumber;
}
return sequence;
}
function getLongestSequence(limit: number) {
let longestSequence = 0;
let longestSequenceStartingNumber = 0;
let startingNumber = 1;
while (startingNumber < limit) {
const sequence = getSequence(startingNumber);
if (sequence.length > longestSequence) {
longestSequence = sequence.length;
longestSequenceStartingNumber = startingNumber;
}
startingNumber++;
}
return [longestSequenceStartingNumber, longestSequence];
}
if (import.meta.main) {
const t0 = performance.now();
const [startingNumber, longest] = getLongestSequence(1_000_000);
const t1 = performance.now();
console.log(
`Found it in ${Math.round(
t1 - t0
)}ms: ${longest} terms for ${startingNumber}`
);
}