Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '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");
- }
- }
- };
Add Comment
Please, Sign In to add comment