3

I am looking at number theory fractals and quite an interesting one is about how, if you color in only the odd numbers of Pascal's Triangle: it starts to look a lot like the Sierpinski Triangle.

I have found the code for generating Pascal's Triangle here Pascal's triangle in tikz. The answer I am using from it is:

 \makeatletter
 \newcommand\binomialCoefficient[2]{%
     % Store values 
     \c@pgf@counta=#1% n
     \c@pgf@countb=#2% k
     %
     % Take advantage of symmetry if k > n - k
     \c@pgf@countc=\c@pgf@counta%
     \advance\c@pgf@countc by-\c@pgf@countb%
     \ifnum\c@pgf@countb>\c@pgf@countc%
         \c@pgf@countb=\c@pgf@countc%
     \fi%
     %
     % Recursively compute the coefficients
     \c@pgf@countc=1% will hold the result
     \c@pgf@countd=0% counter
     \pgfmathloop% c -> c*(n-i)/(i+1) for i=0,...,k-1
         \ifnum\c@pgf@countd<\c@pgf@countb%
         \multiply\c@pgf@countc by\c@pgf@counta%
         \advance\c@pgf@counta by-1%
         \advance\c@pgf@countd by1%
         \divide\c@pgf@countc by\c@pgf@countd%
     \repeatpgfmathloop%
     \the\c@pgf@countc%
 }
 \makeatother

 \begin{document} 
 \begin{tikzpicture}
 \foreach \n in {0,...,15} {
   \foreach \k in {0,...,\n} {
     \node at (\k-\n/2,-\n) {$\binomialCoefficient{\n}{\k}$}; 
   }
 }
 \end{tikzpicture}

I have added an fcolorbox{green}{green}{$\binomialCoefficient{\n}{\k}$}; into the code but obviously that puts a box arounf all of the numbers. How do I add a colored box only to the odd numbers? Thank you.

PercyF2519
  • 1,025
  • As always here, please post a full minimal example that can be copied and tested as is. The choice of document class often matters quite a lot. – daleif Jul 21 '17 at 10:59
  • see this answer also https://tex.stackexchange.com/questions/575092/draw-sierpinskis-triangle-in-pascals-triangle-in-tikz-30-row-or-more – amin Dec 26 '20 at 16:47
  • see this answer also https://tex.stackexchange.com/questions/575092/draw-sierpinskis-triangle-in-pascals-triangle-in-tikz-30-row-or-more – amin Dec 26 '20 at 16:49

1 Answers1

5

Here, I saved \the\c@pgf@countc as \theresult, and then used it later as part of an \ifodd test.

\documentclass{article}
\usepackage{tikz}
 \makeatletter
 \newcommand\binomialCoefficient[2]{%
     % Store values 
     \c@pgf@counta=#1% n
     \c@pgf@countb=#2% k
     %
     % Take advantage of symmetry if k > n - k
     \c@pgf@countc=\c@pgf@counta%
     \advance\c@pgf@countc by-\c@pgf@countb%
     \ifnum\c@pgf@countb>\c@pgf@countc%
         \c@pgf@countb=\c@pgf@countc%
     \fi%
     %
     % Recursively compute the coefficients
     \c@pgf@countc=1% will hold the result
     \c@pgf@countd=0% counter
     \pgfmathloop% c -> c*(n-i)/(i+1) for i=0,...,k-1
         \ifnum\c@pgf@countd<\c@pgf@countb%
         \multiply\c@pgf@countc by\c@pgf@counta%
         \advance\c@pgf@counta by-1%
         \advance\c@pgf@countd by1%
         \divide\c@pgf@countc by\c@pgf@countd%
     \repeatpgfmathloop%
     \xdef\theresult{\the\c@pgf@countc}%
     \theresult%
 }
 \makeatother

 \begin{document} 
 \begin{tikzpicture}
 \foreach \n in {0,...,15} {
   \foreach \k in {0,...,\n} {
     \setbox0=\hbox{$\binomialCoefficient{\n}{\k}$}%
     \ifodd\theresult
       \node [fill=green] at (\k-\n/2,-\n) {\box0}; 
     \else
       \node at (\k-\n/2,-\n) {\box0}; 
     \fi
   }
 }
 \end{tikzpicture}
\end{document}

enter image description here

Alternatively, one could make \binomialCoefficient save but not typeset \theresult. The logic might thus be clearer and avoid the used of temporary boxes.

\documentclass{article}
\usepackage{tikz}
 \makeatletter
 \newcommand\binomialCoefficient[2]{%
     % Store values 
     \c@pgf@counta=#1% n
     \c@pgf@countb=#2% k
     %
     % Take advantage of symmetry if k > n - k
     \c@pgf@countc=\c@pgf@counta%
     \advance\c@pgf@countc by-\c@pgf@countb%
     \ifnum\c@pgf@countb>\c@pgf@countc%
         \c@pgf@countb=\c@pgf@countc%
     \fi%
     %
     % Recursively compute the coefficients
     \c@pgf@countc=1% will hold the result
     \c@pgf@countd=0% counter
     \pgfmathloop% c -> c*(n-i)/(i+1) for i=0,...,k-1
         \ifnum\c@pgf@countd<\c@pgf@countb%
         \multiply\c@pgf@countc by\c@pgf@counta%
         \advance\c@pgf@counta by-1%
         \advance\c@pgf@countd by1%
         \divide\c@pgf@countc by\c@pgf@countd%
     \repeatpgfmathloop%
     \xdef\theresult{\the\c@pgf@countc}%
 }
 \makeatother

 \begin{document} 
 \begin{tikzpicture}
 \foreach \n in {0,...,15} {
   \foreach \k in {0,...,\n} {
     \binomialCoefficient{\n}{\k}%
     \ifodd\theresult
       \node [fill=green] at (\k-\n/2,-\n){$\theresult$};  
     \else
       \node at (\k-\n/2,-\n) {$\theresult$}; 
     \fi
   }
 }
 \end{tikzpicture}
\end{document}