offload/__test__/count-primes.worker.ts

29 lines
427 B
TypeScript
Raw Permalink Normal View History

2024-10-25 21:19:39 +00:00
import { handler } from "../mod.ts";
const isPrime = (n: number) => {
let result = true;
if (n <= 1) {
result = false;
} else {
for (let i = 2; i * i <= n; i++) {
if (n % i === 0) {
result = false;
break;
}
}
}
return result;
};
handler((n: number) => {
let result = 0;
for (let i = 0; i < n; i++) {
if (isPrime(i)) {
result++;
}
}
return result;
});