Created
July 20, 2020 16:12
-
-
Save EsteveSegura/b16b114df2f12d2c74b8669a0b586627 to your computer and use it in GitHub Desktop.
calculate pi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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