September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,20 @@
function mcpi(n) {
var x, y, m = 0;
for (var i = 0; i < n; i += 1) {
x = Math.random();
y = Math.random();
if (x * x + y * y < 1) {
m += 1;
}
}
return 4 * m / n;
}
console.log(mcpi(1000));
console.log(mcpi(10000));
console.log(mcpi(100000));
console.log(mcpi(1000000));
console.log(mcpi(10000000));

View file

@ -0,0 +1,30 @@
(() => {
'use strict';
// monteCarloPi :: Int -> Float
const monteCarloPi = n =>
4 * range(1, n)
.reduce(a => {
const [x, y] = [rnd(), rnd()];
return x * x + y * y < 1 ? a + 1 : a;
}, 0) / n;
// GENERIC FUNCTIONS
// range :: Int -> Int -> [Int]
const range = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
// rnd :: () -> Float
const rnd = Math.random;
// TEST with from 1000 samples to 10E8 samples
return range(3, 8)
.map(x => monteCarloPi(Math.pow(10, x)));
// e.g. -> [3.14, 3.1404, 3.13304, 3.142408, 3.1420304, 3.14156788]
})();

View file

@ -0,0 +1 @@
[3.14, 3.1404, 3.13304, 3.142408, 3.1420304, 3.14156788]

View file

@ -1,18 +0,0 @@
function mcpi(n){
var x,y,m=0;
for(var i = 0; i < n; i += 1) {
x = Math.random();
y = Math.random();
if (x*x + y*y < 1) { m += 1; }
}
return 4*m/n;
}
console.log(mcpi(1000));
console.log(mcpi(10000));
console.log(mcpi(100000));
console.log(mcpi(1000000));
console.log(mcpi(10000000));