-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
68 lines (51 loc) · 1.52 KB
/
index.js
File metadata and controls
68 lines (51 loc) · 1.52 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const evalPI = (coprimes, total) => {
const probability = coprimes / total;
return Math.sqrt((6 / probability))
}
const utilityScript = `
function isCoPrime(a, b) {
return gcd(a, b) === 1;
}
function gcd(a, b) {
a = Math.abs(a);
b = Math.abs(b);
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
`;
const utilBlob = new Blob([utilityScript], {type: "application/javascript"})
const workerScript = `
onmessage = function(e) {
importScripts(e.data.scriptBlob)
const {start, end, max, min} = e.data;
let coprimeCounter = 0;
for(let i = 0; i < end; i++){
const r1 = Math.round(Math.random() * (max - min) + min)
const r2 = Math.round(Math.random() * (max - min) + min)
if(isCoPrime(r1, r2)){
coprimeCounter++;
}
}
postMessage(coprimeCounter)
}
`;
const blob = new Blob([workerScript], {type: "application/javascript"})
const worker = new Worker(URL.createObjectURL(blob))
const run = async () => {
const total = (10 ** 6);
let coprimeCounter = 0;
const chunkSize = 10 ** 6;
for(let i = 0; i < total; i += chunkSize){
const end = Math.min(i + chunkSize, total);
worker.postMessage({ scriptBlob: URL.createObjectURL(utilBlob), start: 1, end, max: 1000, min: 1});
const result = await new Promise(resolve => {
worker.onmessage = e => resolve(e.data);
})
coprimeCounter += result;
}
console.log(evalPI(coprimeCounter, total));
}