Skip to content

Instantly share code, notes, and snippets.

@orleika
Last active October 5, 2015 15:06
Show Gist options
  • Save orleika/e333561e303cf9d7b98a to your computer and use it in GitHub Desktop.
Save orleika/e333561e303cf9d7b98a to your computer and use it in GitHub Desktop.
AGM Method
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