Advertisement
yusufbrima

binomial coefficients

Oct 2nd, 2020
1,622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Latex 1.03 KB | None | 0 0
  1. \documentclass{article}
  2. \usepackage{tikz}
  3.  
  4. \makeatletter
  5. \newcommand\binomialCoefficient[2]{%
  6.     % Store values
  7.     \c@pgf@counta=#1% n
  8.     \c@pgf@countb=#2% k
  9.     %
  10.     % Take advantage of symmetry if k > n - k
  11.     \c@pgf@countc=\c@pgf@counta%
  12.     \advance\c@pgf@countc by-\c@pgf@countb%
  13.     \ifnum\c@pgf@countb>\c@pgf@countc%
  14.         \c@pgf@countb=\c@pgf@countc%
  15.     \fi%
  16.     %
  17.     % Recursively compute the coefficients
  18.     \c@pgf@countc=1% will hold the result
  19.     \c@pgf@countd=0% counter
  20.     \pgfmathloop% c -> c*(n-i)/(i+1) for i=0,...,k-1
  21.         \ifnum\c@pgf@countd<\c@pgf@countb%
  22.         \multiply\c@pgf@countc by\c@pgf@counta%
  23.         \advance\c@pgf@counta by-1%
  24.         \advance\c@pgf@countd by1%
  25.         \divide\c@pgf@countc by\c@pgf@countd%
  26.     \repeatpgfmathloop%
  27.     \the\c@pgf@countc%
  28. }
  29. \makeatother
  30.  
  31. \begin{document}
  32. \begin{tikzpicture}
  33. \foreach \n in {0,...,10} {
  34.  \foreach \k in {0,...,\n} {
  35.    \node at (\k-\n/2,-\n) {$\binomialCoefficient{\n}{\k}$};
  36.  }
  37. }
  38. \end{tikzpicture}
  39.  
  40. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement