# “Arithmetic overflow” error

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
\n=1
\p=1
\loop\ifnum\n<\m \multiply\p by \n \advance\n by 1 \repeat\number\p}}

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

\printfactorials{12}
\end{document}

-
! Arithmetic overflow. \iterate ->\ifnum \n <\m \multiply \p by \n \advance \n by 1 \relax \expanda... l.16 \printfactorials{13} – Andy Apr 10 '12 at 21:32
The largest integer that can be used in TeX is 2^31-1 = 2147483647 and 13! exceeds it. TeX is not a computer algebra system, but a typesetting engine. – egreg Apr 10 '12 at 21:35
Perhaps look at the bigintcalc package, but as @egreg says this is not really the primary purpose of TeX. – Joseph Wright Apr 10 '12 at 21:37

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

\documentclass{article}
\usepackage{bigintcalc}% http://ctan.org/pkg/bigintcalc
\begin{document}
\newcount\n \newcount\p \newcount\m
\def\factorial#1{%
\n=1
\p=1
\loop\ifnum\n<\m \multiply\p by \n \advance\n by 1 \repeat\number\p}}
\def\printfactorials#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{%
\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.

-
You lost a fi :-) – Stephan Lehmke Apr 10 '12 at 22:06
@StephanLehmke: Honestly, I was searching my code for a lost \fi. Thanks! – Werner Apr 12 '12 at 14:07
Sorry if I wasted your time with my joke. Copy-paste from PDF can be a nuisance... – Stephan Lehmke Apr 12 '12 at 14:09

Well mostly the same as Werners answer, but I made an "elementary" implementation of \factorial :-)

\documentclass{article}
\usepackage{bigintcalc}
\usepackage{fullpage}
\begin{document}
\def\factorial#1{{\edef\m{\bigintcalcInc{#1}}%
\def\n{1}%
\def\p{1}%
\loop\ifnum\n<\m\relax\edef\p{\bigintcalcMul\p\n}\edef\n{\bigintcalcInc\n}\repeat\p}}

\def\printfactorials#1{\edef\m{\bigintcalcInc{#1}}%
\def\n{0}%
\loop\ifnum\n<\m\relax \hfil\break\n! = \factorial{\n}
\edef\n{\bigintcalcInc\n}\repeat}
\footnotesize
\printfactorials{70}
\end{document}


Well, in fact it wouldn't be neccessary to use bigintcalc in \printfactorials. \edef\p{\bigintcalcMul\p\n} is really the only place where it is needed.

-