Advertisement
yusufbrima

Even Odd Pascals Triangle

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