Skip to content

Instantly share code, notes, and snippets.

@manonthemat
Created Nov 18, 2017
Embed
What would you like to do?
quick and dirty javascript "lib" to prepare for a statistics exam
'use strict';
var _fstore = [];
function factorial (n) {
if (n == 0 || n == 1) {
return 1;
}
if (_fstore[n] > 0) {
return _fstore[n];
}
return _fstore[n] = factorial(n - 1) * n;
}
module.exports = {
binomial: (n, x, prob) => {
if ((n >= x) && (prob <= 1) && (prob >= 0) && Number.isInteger(n) && Number.isInteger(x) && ( x >= 0)) {
return (factorial(n) / (factorial(n - x) * factorial(x))) * Math.pow(prob, x) * Math.pow(1 - prob, n - x);
} else {
console.log("expected: n, x, p");
}
},
factorial: factorial,
distinguishable_sequence: obj => {
if (obj !== null && typeof obj === 'object') {
let values = Object.values(obj);
if (values.every(Number.isInteger)) {
let n = values.reduce((a, b) => a + b);
let denum = values.reduce((a, b) => "" + a + "! " + b) + "!";
console.log(n + "!");
console.log("-".repeat(denum.length));
console.log(denum);
return factorial(n) / (values.reduce((a, b) => a * factorial(b)));
}
} else {
console.log("expected object with string/integer pairs");
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment