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.

4
  • 2
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Dec 16, 2021 at 20:06
  • This is not the usual shape of Pascal's triangle .
    – Bernard
    Dec 16, 2021 at 20:11
  • @Bernard I want the normal triangle but with those nodes instead of the normal: 1,11,121,1331,... Dec 16, 2021 at 20:12
  • 1
    Maybe you can use this approach to arrange the numbers: tex.stackexchange.com/a/624483/47927 Dec 16, 2021 at 20:53

2 Answers 2

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 ... =)

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}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .