帕斯卡三角形奇数处的颜色

帕斯卡三角形奇数处的颜色

我正在研究数论分形,其中一个相当有趣的现象是,如果你只给帕斯卡三角形的奇数上色,它就会开始看起来很像谢尔宾斯基三角形。

我在这里找到了生成帕斯卡三角形的代码tikz 中的帕斯卡三角形。我从中得到的答案是:

 \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,...,15} {
   \foreach \k in {0,...,\n} {
     \node at (\k-\n/2,-\n) {$\binomialCoefficient{\n}{\k}$}; 
   }
 }
 \end{tikzpicture}

我在代码中添加了一个fcolorbox{green}{green}{$\binomialCoefficient{\n}{\k}$};,但显然这会在所有数字周围放置一个框。如何仅为奇数添加彩色框?谢谢。

答案1

在这里,我保存\the\c@pgf@countc\theresult,然后稍后将其用作\ifodd测试的一部分。

\documentclass{article}
\usepackage{tikz}
 \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%
     \xdef\theresult{\the\c@pgf@countc}%
     \theresult%
 }
 \makeatother

 \begin{document} 
 \begin{tikzpicture}
 \foreach \n in {0,...,15} {
   \foreach \k in {0,...,\n} {
     \setbox0=\hbox{$\binomialCoefficient{\n}{\k}$}%
     \ifodd\theresult
       \node [fill=green] at (\k-\n/2,-\n) {\box0}; 
     \else
       \node at (\k-\n/2,-\n) {\box0}; 
     \fi
   }
 }
 \end{tikzpicture}
\end{document}

在此处输入图片描述

或者,可以进行\binomialCoefficient保存但不进行排版\theresult。这样逻辑可能更清晰,并避免使用临时框。

\documentclass{article}
\usepackage{tikz}
 \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%
     \xdef\theresult{\the\c@pgf@countc}%
 }
 \makeatother

 \begin{document} 
 \begin{tikzpicture}
 \foreach \n in {0,...,15} {
   \foreach \k in {0,...,\n} {
     \binomialCoefficient{\n}{\k}%
     \ifodd\theresult
       \node [fill=green] at (\k-\n/2,-\n){$\theresult$};  
     \else
       \node at (\k-\n/2,-\n) {$\theresult$}; 
     \fi
   }
 }
 \end{tikzpicture}
\end{document}

相关内容