4

The tetrahedral numbers count the number of balls with equal radii needed to create a triangular pyramid with n layers. The nth tetrahedral number is given by the formula C(n + 2, 3). Accordingly, the tetrahedral numbers appear as a diagonal in Pascal's triangle. I wish to highlight those numbers in Pascal's triangle. However, what my attempt produced was Pascal's triangle with a separate row in which the highlighted numbers appeared in red. How do I modify my code so that the tetrahedral numbers are highlighted in the triangle itself?

tetrahedral_numbers

Here is the code, which is based on Caramdir's answer to this question about how to present the binomial coefficients in Pascal's triangle in numerical form:

\documentclass{article}
\usepackage{tikz}

%calculate binomial coefficients 
\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{figure}[h]
\centering
\begin{tikzpicture}
        \foreach \n in {0, ..., 7} {
            \foreach \k in {0,...,\n} {
                \node (\n\k) at (\k-\n/2,-\n) {\(\binomialcoefficient{\n}{\k}\)};
                \foreach \n in {3, 4, ..., 7} \node[color = red] at (\n, 3) {\(\binomialcoefficient{\n}{3}\)};
            }
            \pgfmathtruncatemacro{\x}{(\n+1)/2}
            \pgfmathtruncatemacro{\y}{\n/2}
        }
\end{tikzpicture}
\end{figure}

\end{document}
3
6

The tetrahedral numbers in your algorithm are identified by the fact that the difference between the \n index and the \k index is equal to 3.

So, just calculate this difference and color them as here :

\pgfmathparse{int(\n-\k)}
\ifnum \pgfmathresult=3
\node[red,node font=\bf] (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
\else
\node (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
\fi

screenshot

\documentclass{article}
\usepackage{tikz}

%calculate binomial coefficients 
\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{figure}[h]
\centering
\begin{tikzpicture}
        \foreach \n in {0, ...,7} {
            \foreach \k in {0,...,\n} {
             \pgfmathparse{int(\n-\k)}
             \ifnum \pgfmathresult=3
             \node[red,node font=\bf] (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
             \else
             \node (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
             \fi
%                \foreach \n in {3, 4, ..., 7} \node[color = red] at (\n, 3) {\(\binomialcoefficient{\n}{3}\)};
            }
            \pgfmathtruncatemacro{\x}{(\n+1)/2}
            \pgfmathtruncatemacro{\y}{\n/2}
        }
\end{tikzpicture}
\end{figure}

\end{document}
1
  • 1
    A nice solution. Thank you. Jul 17 '20 at 11:41
3

AndreC was too fast for me :) Just an alternative to evaluate directly \x, \y and \n-\k directly in the loop and avoid \pgfmathtruncatemacro

\documentclass{standalone}
\usepackage{tikz}

%calculate binomial coefficients 
\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, ..., 11} 
    {
        \foreach [evaluate ={
                \x = int(0.5*(\n+1));
                \y = int(0.5*\n) ;
                \NmK = int(\n-\k) ; %n minus k, NmK
            }] \k in {0,...,\n} 
        {
            \ifnum  \NmK=3          
                \node[red,node font=\bf] (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
            \else
                \node (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
            \fi             

        }
    }
\end{tikzpicture}

\end{document}
1
  • 1
    A nice solution. Thank you. Jul 17 '20 at 11:42
2

The nice solutions by AndreC and JeT both highlight the C(n + 2, n - 1) entries, with n being at least 1. Since I was interested in highlighting the C(n + 2, 3) entries, again with n being at least 1, I modified AndreC's code to highlight the opposite diagonal as follows:

\documentclass{article}
\usepackage{tikz}

%calculate binomial coefficients 
\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{figure}[h]
\centering
\begin{tikzpicture}
        \foreach \n in {0, ...,7} {
            \foreach \k in {0,...,\n} {
             \ifnum \k=3
             \node[red,node font=\bf] (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
             \else
             \node (\n\k) at (\k-\n/2,-\n) {\binomialcoefficient{\n}{\k}};
             \fi
%                \foreach \n in {3, 4, ..., 7} \node[color = red] at (\n, 3) {\(\binomialcoefficient{\n}{3}\)};
            }
            \pgfmathtruncatemacro{\x}{(\n+1)/2}
            \pgfmathtruncatemacro{\y}{\n/2}
        }
\end{tikzpicture}

\end{figure}

\end{document}

Pascal's_triangle_with_the_tetrahedral_numbers_highlighted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.