6

I am writing some material on the trinomial theorem and Pascal's pyramid, and I'm trying to typeset a single layer. Here is my code so far:

\documentclass{article}

\usepackage{tikz}

\begin{document}
\pgfkeys{/pgf/number format/.cd,int trunc}

\small
\begin{tikzpicture}
  \pgfmathsetmacro{\n}{8}
  \foreach \i in {0,1,...,\n}
  {
    \foreach \j in {0,...,\i}
    {
      \draw (\j-\i*0.5,\i*0.866,) -- ++(1,0) -- ++(-0.5,-0.866) -- cycle;
      \node[align=center] at (0.1+\j-\i*0.5,-0.65+\i*0.866)
      {\pgfmathparse{factorial(\n)/factorial(\n-\i)%
       /factorial(\j)/factorial(\i-\j)}\pgfmathprintnumber{\pgfmathresult}};
    }
  }
\end{tikzpicture}

\end{document}

This works fine up to n=7, but crashes with an arithmetic overflow on n=8. As you can see, the computation is simply finding the values of some multinomial coefficients. But why the crash on n=8? And what's the best way around it?

Alasdair
  • 5,257
  • 4
  • 38
  • 64
  • 1
    I see. You are right\pgfmathparse{factorial(8)}\pgfmathresult; and also \pgfmathparse{8!}\pgfmathresult; does not work for some reasons. (also all values greater doesn't work for me) Error: ! Arithmetic overflow. \pgfmath@iterate ...pgfmath@x by\c@pgfmath@counta \advance \c@pgfmath@counta... l.25 \pgfmathparse{factorial(8)} \pgfmathresult; I can't carry out that multiplication or division, since the result is out of range. – Bobyandbob Aug 04 '17 at 05:10
  • 1
    You might get help by https://tex.stackexchange.com/q/17522/95544. A couple of the answers implement the binomial coefficient. – StefanH Aug 04 '17 at 07:43
  • Ah! page 923 of the TiKZ/pgf manual: "calculations must not exceed ±16383.99999 at any point," Well, that explains it. So clearly I need another package such as xint for my computations, or farm my computations to an external package such as GNU Maxima or Sage. – Alasdair Aug 04 '17 at 11:33
  • 1
    This would be trivial with LuaLaTeX...... – JPi Aug 04 '17 at 12:21
  • 1
    I'm sure that LuaLaTeX may well be the answer to my dreams... but in the end I used xint instead of pgfmath, and computed the numbers I wanted with {\xinttheexpr binomial(\n,\i)*binomial(\i,\j)\relax}. – Alasdair Aug 04 '17 at 12:41
  • @Alasdair could you add your solution as an answer? – Bobyandbob Aug 19 '18 at 07:35

0 Answers0