Last active
October 5, 2015 15:06
-
-
Save orleika/e333561e303cf9d7b98a to your computer and use it in GitHub Desktop.
AGM Method
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
var pi = agm(3); | |
console.log(pi); | |
function agm(n) { | |
var a = 1, | |
b = 1 / Math.sqrt(2), | |
t = 1 / 4, | |
p = 1, | |
an; | |
for (var i = 0; i < n; i++) { | |
an = (a + b) / 2; | |
b = Math.sqrt(a * b); | |
t = t - p * (Math.pow(a, 2) - 2 * a * an + Math.pow(an, 2)); | |
p = 2 * p; | |
a = an; | |
} | |
return Math.pow(a + b, 2) / (4 * t); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment