Saturday, December 10, 2016

half-baked pi

PI is a Strange Loop

(function aPieceOfPi(numerator, denominator) {
  var n = numerator;
  var d = denominator;
  try {
    return n / d * n / (d + 2) * aPieceOfPi(n + 2, d + 2);
  }
  catch(stackoverflow) {
    console.log(stackoverflow.message);
    return 1;
  }
})(2, 1)

ROBOT:I'll explain while...

(function anotherPieceOfPi(numerator, denominator, iterations) {
  var piece = 1;
  var n = numerator;
  var d = denominator;
  while (iterations-- > 0) {
    piece = piece * n / d * n / (d + 2);
    n += 2;
    d += 2;
  }
  return piece;
})(2, 1, 100000000)