Created
December 31, 2023 15:41
-
-
Save davidsharp/4b2deff51159da05753c433475156d1a to your computer and use it in GitHub Desktop.
pi approximation in JS
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
const approximate_pi = n => { | |
let pi = 0 | |
let denom = 1 | |
for(let i = 0;i<n;i++){ | |
pi += (i%2?-4:4)/denom | |
denom += 2 | |
} | |
return pi | |
} | |
let dec = 10 | |
console.log(`100 turns: `,approximate_pi(100).toFixed(dec)) | |
console.log(`1000 turns: `,approximate_pi(1000).toFixed(dec)) | |
console.log(`10000 turns: `,approximate_pi(10000).toFixed(dec)) | |
console.log(`100000 turns: `,approximate_pi(100000).toFixed(dec)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment