-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworker.ts
More file actions
40 lines (30 loc) · 700 Bytes
/
Copy pathworker.ts
File metadata and controls
40 lines (30 loc) · 700 Bytes
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
const ctx: Worker = self as any
const countPrimes = function (n: number) {
if (n <= 1) {
return 0
}
const dp = new Array(n).fill(true)
for (let num = 2; num < Math.sqrt(n); num++) {
if (dp[num] === false) {
continue
}
for (let i = num * num; i < n; i += num) {
dp[i] = false
}
}
let count = 0
for (let i = 2; i < n; i++) {
if (dp[i] === true) {
count++
}
}
return count
}
ctx.onmessage = async (e) => {
if (!e.data) {
return 0
}
const count = countPrimes(e.data)
ctx.postMessage({ message: count })
}
// ctx.addEventListener('message', )