Skip to content

Instantly share code, notes, and snippets.

@EsteveSegura
Created July 20, 2020 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EsteveSegura/b16b114df2f12d2c74b8669a0b586627 to your computer and use it in GitHub Desktop.
Save EsteveSegura/b16b114df2f12d2c74b8669a0b586627 to your computer and use it in GitHub Desktop.
calculate pi
let r = 5;
let points_total = 0;
let points_inside = 0;
while (1) {
points_total++;
let x = Math.random() * r * 2 - r
let y = Math.random() * r * 2 - r
if (Math.pow(x, 2) + Math.pow(y, 2) < Math.pow(r, 2)) {
points_inside++;
}
if (points_total % 10000 == 0) {
console.log(`${points_inside} / ${points_total} = ${4 * points_inside / points_total}`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment