Skip to content

Instantly share code, notes, and snippets.

@rstacruz
Created March 28, 2017 08:19
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 rstacruz/f74c872a3b9f474cc24de4f2d00e9ec6 to your computer and use it in GitHub Desktop.
Save rstacruz/f74c872a3b9f474cc24de4f2d00e9ec6 to your computer and use it in GitHub Desktop.
Approximating pi
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