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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more