Skip to content

Instantly share code, notes, and snippets.

@manonthemat
Created November 18, 2017 16:17
Show Gist options
  • Save manonthemat/9a5abf685903b736e7ca1a345e5b0958 to your computer and use it in GitHub Desktop.
Save manonthemat/9a5abf685903b736e7ca1a345e5b0958 to your computer and use it in GitHub Desktop.
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