[Tex/LaTex] “Arithmetic overflow” error

calculationscounters

factorials up to 12 are handled.How can make it to compute beyond that?

Error: “Arithmetic overflow

\documentclass{article}  
\begin{document} 
\newcount\n \newcount\p \newcount\m
\def\factorial#1{{\m=#1\advance\m by 1
\n=1
\p=1
\loop\ifnum\n<\m \multiply\p by \n \advance\n by 1 \repeat\number\p}}

\def\printfactorials#1{\m=#1\advance\m by 1
\n=0
\loop\ifnum\n<\m \hfil\break\number\n! = \factorial{\n} \advance\n by 1 \repeat}

\printfactorials{12}
\end{document} 

Best Answer

Using bigintcalc (from the oberdiek bundle), you can obtain the following output:

enter image description here

\documentclass{article}
\usepackage{bigintcalc}% http://ctan.org/pkg/bigintcalc
\begin{document} 
\newcount\n \newcount\p \newcount\m
\def\factorial#1{%
  {\m=#1\advance\m by 1
   \n=1
   \p=1
   \loop\ifnum\n<\m \multiply\p by \n \advance\n by 1 \repeat\number\p}}
\def\printfactorials#1{%
  \m=#1\advance\m by 1
  \n=0
  \loop\ifnum\n<\m \hfil\break\number\n! = \factorial{\n} \advance\n by 1 \repeat}

\def\bigfactorial#1{%
  \bigintcalcFac{#1}%
}
\def\printbigfactorials#1{%
  \m=#1\advance\m by 1
  \n=0
  \loop\ifnum\n<\m \hfil\break\number\n! = \bigfactorial{\the\n} \advance\n by 1 \repeat}

%\printfactorials{12}
\printbigfactorials{20}
\end{document} ​

\bigintcalcXXX is the basic operators that are defined. \bigintcalcFac{<x>} returns the factorial of <x>. According to the bigintcalc documentation,

Package bigintcalc defines arithmetic operations that deal with big integers. Big integers can be given either as explicit integer number or as macro code that expands to an explicit number. Big means that there is no limit on the size of the number. Big integers may exceed TeX's range limitation of -2147483647 and 2147483647. Only memory issues will limit the usable range.