math-stats
Version:
Statistics for your numbers
30 lines (22 loc) • 675 B
JavaScript
;
/*global module:true*/
// Calculating nth root of 'a'
let nthRoot = (n, a) => {
if (n === 2)
return Math.sqrt(a).toPrecision(6);
else if (n === 3)
return Math.cbrt(a).toPrecision(6);
let preResult = Math.random() % 10; // Initial guess
let eps = Math.pow(10, -6); // Lesser the eps value, more the accuracy
let delX = Number.MAX_SAFE_INTEGER;
let result;
while (delX > eps) {
result = ((n - 1.0) * preResult + a/Math.pow(preResult, n-1)) / n;
delX = Math.abs(result - preResult);
preResult = result;
}
return result.toPrecision(6);
};
module.exports = {
nthRoot: nthRoot
};