-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.ts
32 lines (30 loc) · 1 KB
/
dijkstra.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
import { Heap } from 'heap-js'
import type { Comparator } from 'heap-js'
type Tuple = [number, number]
const keyComparator: Comparator<Tuple> = (a, b) => a[1] - b[1]
export function dijkstra(num: number): [number[], number] {
const t = performance.now()
const pool = new Heap(keyComparator)
pool.init([[2, 4]])
const primes: number[] = []
for (let i = 2; i <= num; i++) {
while (pool.get(0)[1] < i) {
const top: Tuple | undefined = pool.pop()
if (top !== undefined) {
const [prime, multiple] = top
pool.push([prime, multiple + prime])
}
}
if (pool.get(0)[1] === i) {
const top: Tuple | undefined = pool.pop()
if (top !== undefined) {
const [prime, multiple] = top
pool.push([prime, multiple + prime])
}
} else {
primes.push(i)
pool.push([i, i * i])
}
}
return [primes, performance.now() - t]
}