Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. const factor = Math.pow(10,6);
  4.  
  5. function combination(n, m) {
  6.  
  7. return factorial(n) / (factorial(m) * factorial(n - m));
  8. }
  9.  
  10.  
  11. function factorial(n) {
  12. if (n == 0) {
  13. return 1;
  14. } else {
  15. return factorial(n - 1) * n;
  16. }
  17. }
  18.  
  19. function prob(n, m, target, other) {
  20. return combination(n, m) * Math.pow(target, m) * Math.pow(other,n - m);
  21. }
  22.  
  23.  
  24.  
  25. function sum(n, a, b) {
  26. let total = 0;
  27. for (var i = 0; i < n + 1; i++) {
  28. let p = prob(n,i, a, b);
  29. let pp = Math.round(p * factor)/ factor;
  30. total += pp;
  31. console.log(`${i}\t${pp}\t${total}`);
  32. }
  33.  
  34. }
  35.  
  36.  
  37. sum(20, 1/75, 74/75);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement