# draw Sierpinski's triangle in Pascal's triangle in tikz (30 row or more)

I want to draw Sierpinski's triangle in Pascal's triangle in tikz for 30 rows or more but my code only works for 13 rows.

\documentclass{article}
\usepackage{tikz}
\usepackage{pdfpages}
\begin{document}
\begin{center}
\newdimen\R
\R=.4cm
\newcommand\mycolor{white}
\newcommand\thik{\pgflinewidth}
\begin{tikzpicture}[line width=.8pt]
\foreach \k in {0,...,13}{
\begin{scope}[shift={(-60:{sqrt(3)*\R*\k})}]
\pgfmathtruncatemacro\ystart{13-\k}
\foreach \n in {0,...,\ystart}{
\pgfmathtruncatemacro\newn{\n+\k}
%binomila ceeficients
\pgfmathsetmacro{\value}{1};
\ifthenelse{\k>0}{
\foreach \b in {1,...,\k}{
\pgfmathtruncatemacro{\value}{\value*(\newn-\b+1)/\b)};
\global\let\value=\value
}
}{};
\pgfmathtruncatemacro{\rest}{mod(\value,2)};
\ifthenelse{\rest=0}{\def\mycolor{green}}{}

\begin{scope}[shift={(-120:{sqrt(3)*\R*\n})}]
\draw[top color=\mycolor!20,bottom color=\mycolor!60]
(30:\R) \foreach \x in {90,150,...,330} {
-- (\x:\R)}
--cycle (90:0) node {\tiny $\mathbf{\value}$};
\end{scope}
}
\end{scope}
}
\end{tikzpicture}
\end{center}
\end{document}

• Hello ! Your examples is very weird. I does not compile on my system. It works only if I remove the center environement. But it requires pdfpages, which doesn't seem to be required AFAIK. I have the feeling something is not right here ... and this shows up if you got to 30 but not with 13. Dec 15, 2020 at 11:03
• @BambOo Delete the invisible character becore and after \end{center}, then it compiles. Dec 15, 2020 at 11:11
• The problem is the line \pgfmathtruncatemacro{\value}{\value*(\newn-\b+1)/\b)}; TikZ uses TeX dimensions, and these registers can only hold values ±16383.99999. See e.g. the article \pgfmathsetmacro - dimension too large (biggest value allowed?) Dec 15, 2020 at 11:14
• As noted there, you can rewrite the calculations using pgfplots and the fpu library. Dec 15, 2020 at 11:18
• @gernot, I do not think the problem comes from an invisible character. If I try to compile the document inside a [tikz]{standalone} class it does not work as well ... as I said, very weird Dec 15, 2020 at 11:19

Welcome to SE! This was really fun.

I reacquainted myself with fp.sty. I have also formatted the code to be a little easier to follow (indentation is your friend!). I have tried this with as many as 35 rows -- more are certainly possible. Note, however, that as the number of rows increases, so does the time necessary to compile this. I've also made it into a macro for easy experimentation -- and entertainment!

# UPDATE!

I got up early this morning to play with my new toy. I was greeted with a TeX error: Dimension too large. Odd because I have the output graphics to prove that the code worked just fine yesterday. Apparently, overnight, PGF decided that it would choke using mod. Not to be deterred, I found \modulo at How do I calculate n modulo 3 in LaTeX?, which I have employed here. Thank you @Werner! Long live StackExchange!!

The output was incorrect -- that has been corrected as well.

\documentclass[tikz,border=0.25in]{standalone}

\usepackage{fp}

\newlength{\R}
\setlength{\R}{.75cm}
\newcommand\mycolor{white}

%% https://tex.stackexchange.com/questions/34424/how-do-i-calculate-n-modulo-3-in-latex/34425#34425
\newcommand{\modulo}[2]{%
\FPeval{\result}{trunc(#1-(#2*trunc(#1/#2,0)),0)}\result%
}

%%  #1 determines which hexagons are filled (default is 2); #2 is number of rows
\newcommand{\makesierp}[2][2]{%
\begin{tikzpicture}[line width=.8pt]
\foreach \k in {0,...,#2}{
\begin{scope}[shift={(-60:{sqrt(3)*\R*\k})}]
\pgfmathtruncatemacro\ystart{#2-\k}
\foreach \n in {0,...,\ystart}{
\pgfmathtruncatemacro\newn{\n+\k}
\pgfmathsetmacro{\myvalue}{1}
\ifnum\k>0
\foreach \b in {1,...,\k}{
\FPeval\myvalue{trunc(\myvalue*(\newn-\b+1)/\b:0)}
\global\let\myvalue=\myvalue
}
\fi
\modulo{\myvalue}{#1}%
\ifnum\result=0 \def\mycolor{green}\fi%
\begin{scope}[shift={(-120:{sqrt(3)*\R*\n})}]
\draw[fill=\mycolor!20]
(30:\R) \foreach \x in {90,150,...,330} {
-- (\x:\R)}
--cycle (90:0)node[font=\tiny] {$\mathbf{\myvalue}$};
\end{scope}
}
\end{scope}
}
\end{tikzpicture}
}

\begin{document}

\makesierp{20}

\end{document}

The second graphic was generated with \makesierp[5]{20}

Have fun!

• thanks a lot. It's very useful for me and very nice.
– amin
Dec 16, 2020 at 19:04