Published
Edited
Jul 8, 2018
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
l2_norm = (vector) => Math.sqrt(vector.map(x => x * x).reduce((sum, elem) => sum + elem, 0))
Insert cell
Insert cell
function estimate_pi(num_darts)
{
let inside_circle_count = 0;
// start throwing darts
for(var i = 0; i < num_darts; i++)
{
// we're actually going to use a quarter circle for this example
// Math.random() outputs a number on the interval [0,1) so this makes a point [0,1)x[0,1)
let point = [Math.random(), Math.random()];
// if the point is actually within 1 unit of the origin, it's inside the circle
if(l2_norm(point) <= 1) inside_circle_count++;
}
return 4 * inside_circle_count / num_darts;
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more