3

I am writing a document and would love to write Pascal's triangle modulo 2. That is each binomial coefficient modulo 2, "the remainder" when dividing by 2. I found these links (Pascal) of the code for the normal triangle, and tried to just write manually each node, but I just didn't manage to do it. I want a triangle like:

1
11
101
1111
10001
...

Some help would be really appreciated, thank you very much.

David Carlisle
  • 757,742

2 Answers2

5

You can probably simplify the logic a bit but

enter image description here

\documentclass{article}

\def\zz#1#2{% \ifx\empty#2\else \ifodd\numexpr#1+#2\relax1\else0\fi \expandafter\zz\expandafter#2% \fi}

\def\ptriangle#1#2{% \ifnum#1=0 \def\lastline{1}% \else \edef\lastline{\expandafter\zz\expandafter0\lastline0\empty}% \fi \lastline\par \ifnum#1=#2 \expandafter\zzstop \fi \ptriangle{\numexpr#1+1\relax}{#2}% } \def\zzstop#1#2#3{} \begin{document}

\ptriangle{0}{5}

\end{document}

David Carlisle
  • 757,742
5

Actually, you can just take any example of pascals triangle that you can find on this site and apply mod(2) on the numbers, For example, taking this approach we can get:

\documentclass[tikz,border=5]{standalone}
\usepgflibrary{fpu}
\begin{document}
\def\N{10}
\tikz[x=0.75cm,y=0.5cm, 
  pascal node/.style={font=\footnotesize}, 
  row node/.style={font=\footnotesize, anchor=west, shift=(180:1)}]
  \path  
    \foreach \n in {0,...,\N} { 
      (-\N/2-1, -\n) node[row node/.try]{Row \n:}
        \foreach \k in {0,...,\n}{
          (-\n/2+\k,-\n) node[pascal node/.try] {%
            \pgfkeys{/pgf/fpu}%
            \pgfmathparse{mod(round(\n!/(\k!*(\n-\k)!)),2)}%
            \pgfmathfloattoint{\pgfmathresult}%
            \pgfmathresult%
          }}};
\end{document}

enter image description here

The single minus zero in the last line is probably the result of a rounding error. I leave it to you to fix it ... =)