Skip to content

Commit

Permalink
added nilakantha.js for the calculation of PI
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Nov 23, 2022
1 parent 710587c commit 06d4d87
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions nilakantha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const add = require("./add.js");
const long_addition = require("./long_addition.js");
const divide = require("./divide.js");
const multiply = require("./multiply.js");

// calculate PI using Nilakantha Series
function nilakantha(steps = 100, { divide_options } = {}) {
let sign = "+";
let pi = "3";
let a = "2";
let b = "3";
let c = "4";
for (let i = 1; i < steps; i++) {
const divisor = multiply(a, b, c);

const part = sign + divide("4", divisor, divide_options);

pi = add(pi, part);

// flip sign
sign = sign === "-" ? "+" : "-";

a = c;
b = long_addition(c, "1");
c = long_addition(b, "1");
}
return pi;
}

module.exports = nilakantha;
module.exports.default = nilakantha;

0 comments on commit 06d4d87

Please sign in to comment.