Created
March 28, 2017 08:19
-
-
Save rstacruz/f74c872a3b9f474cc24de4f2d00e9ec6 to your computer and use it in GitHub Desktop.
Approximating 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
const PHI = (Math.sqrt(5) + 1) / 2 | |
function divisor (n) { | |
if (n === 0) return Math.sqrt(2 + PHI) | |
var last = divisor(n - 1) | |
return Math.sqrt(2 + last) | |
} | |
function pi (n) { | |
if (n === 0) return 5 / PHI | |
var last = pi(n - 1) | |
return last * 2 / divisor(n) | |
} | |
console.log(pi(50)) | |
//=> 3.1415926535897927 | |
console.log(Math.PI) | |
//=> 3.141592653589793 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment