summaryrefslogtreecommitdiff
path: root/godel/js/godelWorker.js
blob: 594a4adddee7ef81e9f24a7d8b6dd3681a3eaa15 (plain)
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
const isPrime = (n) =>
  !Array(Math.ceil(Math.sqrt(n)))
    .fill(0)
    .map((_, i) => i + 2) // first prime is 2
    .some((i) => n !== i && n % i === 0);

const primesCache = [2];
const p = (i) => {
  if (primesCache.length <= i) {
    let x = primesCache.at(-1);
    while (primesCache.length <= i) {
      if (isPrime(++x)) primesCache.push(x);
    }
  }
  return primesCache.at(i - 1);
};

const computeGodelNumber = (godelSequence) =>
  godelSequence.reduce((acc, num, i) => {
    const prime = p(i + 1);
    return BigInt(acc) * BigInt(prime) ** BigInt(num);
  }, 1) - BigInt(1);

self.addEventListener("message", (e) => {
  const godelNumber = computeGodelNumber(e.data);
  postMessage(godelNumber);
});