Skip to content

Instantly share code, notes, and snippets.

@lysenko-sergey-developer
Created March 1, 2020 14:41
Show Gist options
  • Save lysenko-sergey-developer/1891b2c9954e545b47d61a4572675822 to your computer and use it in GitHub Desktop.
Save lysenko-sergey-developer/1891b2c9954e545b47d61a4572675822 to your computer and use it in GitHub Desktop.
function estimatePi(n) {
let numPointCircle = 0;
let numPointTotal = 0;
for (let i = 0; i < n; i++) {
const x = Math.round(Math.random())
const y = Math.round(Math.random())
let distance = x ** 2 + y ** 2;
if (distance <= 1) {
numPointCircle++;
}
numPointTotal++;
}
return 4 * numPointCircle / numPointTotal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment