# How to draw Pascal's Triangle mod 3 with Tikz

I am writing about a math problem in which Pascal's Triangle is being studied (mod 3). Therefore I took the code from another thread in which the first n lines of Pascal's triangle were calculated. Then I found the intcalc package from which I took the \intcalcMod{}{} command so that it just returned the value (mod 3). My code ended up looking like this :

\usepackage{tikz}
\usepackage{intcalc}

\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%
\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%
\divide\c@pgf@countc by\c@pgf@countd%
\repeatpgfmathloop%
\the\c@pgf@countc%
}
\makeatother
\begin{document}
\begin{tikzpicture}
\foreach \n in {0,...,5} {
\foreach \k in {0,...,\n} {
\node at (\k-\n/2,-\n) {$\intcalcMod{\binomialCoefficient{\n}{\k}}{3}$};
}
}
\end{tikzpicture}
\end{document}


It worked without the \intcalcMod{}{3} around the \binomialCoefficient{\n}{\k}, however now it gives this result: How can this be fixed ? Thank you for any help.

• Welcome! Please can you make your code so it can be copy-paste-compiled? When I guess, I get an error. If I ignore the error, I don't get the output you show.
– cfr
Commented May 18 at 13:56

The \binomialcoefficient macro is not expandable: it has all the computations and can be used for printing the value, not for passing it for further computations.

You can do the computation and use the stored value to feed \intcalcMod.

\documentclass{article}

\usepackage{tikz}
\usepackage{intcalc}

\makeatletter
\newcommand\computebinomialCoefficient[2]{%
% Store values
\c@pgf@counta=#1\relax% n
\c@pgf@countb=#2\relax% k
%
% Take advantage of symmetry if k > n - k
\c@pgf@countc=\c@pgf@counta
\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
\divide\c@pgf@countc by\c@pgf@countd
\repeatpgfmathloop
%\the\c@pgf@countc%
}
\makeatother

\begin{document}

\begin{tikzpicture}
\foreach \n in {0,...,5} {
\foreach \k in {0,...,\n} {
\computebinomialCoefficient{\n}{\k}%
\node at (\k-\n/2,-\n) {$\intcalcMod{\value{pgf@countc}}{3}$};
}
}
\end{tikzpicture}

\end{document}


I fixed all the wrongly placed % in the code.

You can use a different strategy, though:

\documentclass{article}

\usepackage{tikz}
\usepackage{xintexpr}

\begin{document}

\begin{tikzpicture}
\foreach \n in {0,...,5} {
\foreach \k in {0,...,\n} {
\node at (\k-\n/2,-\n) {$\xintexpr\xintiiBinomial{\n}{\k}/:3\relax$};
}
}
\end{tikzpicture}

\end{document}


Look at the manual for xintexpr for more details.

• Thank you so much for your help! I'd only used TeX for short solutions to math problems, so I was a little lost. : ) Commented May 18 at 14:45

With lualatex

%!TEX TS-program = lualatex
\documentclass{article}
\usepackage{tikz,luacode}

\begin{document}
\begin{luacode}
function  Coeff (n,k)
local Bnk = 1
for i = 1,k do
Bnk = (n-i+1)*Bnk / i
end
return  math.fmod(Bnk,3)
end
\end{luacode}

\begin{tikzpicture}
\pgfkeys{/pgf/number format/.cd,fixed,precision=0}
\foreach \n in {0,...,8} {
\foreach \k in {0,...,\n} {
\node at (\k-\n/2,-\n) { \pgfmathprintnumber{\directlua{ tex.print(Coeff(\n,\k)) }}};
}}
\end{tikzpicture}
\end{document}