Guest User

Untitled

a guest
Nov 20th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var _fstore = [];
  4. function factorial (n) {
  5. if (n == 0 || n == 1) {
  6. return 1;
  7. }
  8. if (_fstore[n] > 0) {
  9. return _fstore[n];
  10. }
  11. return _fstore[n] = factorial(n - 1) * n;
  12. }
  13.  
  14. module.exports = {
  15. binomial: (n, x, prob) => {
  16. if ((n >= x) && (prob <= 1) && (prob >= 0) && Number.isInteger(n) && Number.isInteger(x) && ( x >= 0)) {
  17. return (factorial(n) / (factorial(n - x) * factorial(x))) * Math.pow(prob, x) * Math.pow(1 - prob, n - x);
  18. } else {
  19. console.log("expected: n, x, p");
  20. }
  21. },
  22. factorial: factorial,
  23. distinguishable_sequence: obj => {
  24. if (obj !== null && typeof obj === 'object') {
  25. let values = Object.values(obj);
  26. if (values.every(Number.isInteger)) {
  27. let n = values.reduce((a, b) => a + b);
  28. let denum = values.reduce((a, b) => "" + a + "! " + b) + "!";
  29. console.log(n + "!");
  30. console.log("-".repeat(denum.length));
  31. console.log(denum);
  32. return factorial(n) / (values.reduce((a, b) => a * factorial(b)));
  33. }
  34. } else {
  35. console.log("expected object with string/integer pairs");
  36. }
  37. }
  38. };
Add Comment
Please, Sign In to add comment