Advertisement
cirossmonteiro

Pascal's Triangle

Apr 20th, 2022
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // recursive formula for factorial
  2. const factorial = (n = 0) => {
  3.   return n === 0 ? 1 : n*factorial(n-1);
  4. }
  5.  
  6. // https://en.wikipedia.org/wiki/Binomial_coefficient
  7. const binomial_coefficient = (n = 0, k = 0) => {
  8.   return factorial(n)/factorial(k)/factorial(n-k);
  9. }
  10.  
  11. // https://en.wikipedia.org/wiki/Pascal%27s_triangle#Formula
  12. const pascal = (N = 8) => {
  13.   for(let i = 0; i < N; i++) {
  14.     let line = "";
  15.     for(let j = 0; j <= i; j++) {
  16.       line += ` ${String(binomial_coefficient(i, j))}`;
  17.     }
  18.     console.log(line);
  19.   }
  20. }
  21.  
  22. N = parseInt(process.argv[2] || 8);
  23.  
  24. pascal(N);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement