# Integer arithmetic in TeX

After many years of being merely a regular TeX (LaTeX lately) I started reading little bit more recently. I wrote a small program (very naive) which is suppose to illustrate doing integer arithmetic in TeX.

\message{Please enter the first number:}

The first entered number is \first

\count0=\first

\message{Please enter the second number:}

\count1=\second

The second entered number is \second

Could you please compare the sum of number you have entered and
the number of the page :)
\bye


Is there a simple way to recover the content of register \count0 on an arbitrary place on the page?

-
You find the total at the bottom of the page, as Plain TeX uses \count0 for assigning the value to the page number. :) –  egreg Dec 27 '11 at 11:11
I think you have not read my example carefully :) –  Predrag Punosevac Dec 27 '11 at 13:40
Just a joke to advise you about being careful in not using \count0 outside of a group. ;-) –  egreg Dec 27 '11 at 13:43
I apologize for being a slow student :) I promise I will try to read comments more carefully and try to think before I replay :) –  Predrag Punosevac Dec 27 '11 at 14:25

In order to obtain \count0, use \the\count0. This will typeset the value of \count0 wherever it is used.

\message{Please enter the first number:}

The first entered number is \first

\count0=\first

\message{Please enter the second number:}

\count1=\second

The second entered number is \second

Could you please compare the sum of number you have entered and
the number of the page :) \the\count0
\bye

-

To complete the answer of Werner :

You can also use \number. If \count213=1789 then \the\count213and \number\count213 are equivalent, but if you can write \number2012 it's not possible to write \the2012 or \the{2012} .

If you want to learn something about integers with TeX, you can look at the sources of the TeXBook, for example Knuth writes this code :

\newif\ifprime \newif\ifunknown
\newcount\n \newcount\p \newcount\d \newcount\a
\def\primes#1{2,~3% assume that #1 is at least 3
\n=#1 \advance\n by-2 % n more to go
\p=5 % odd primes starting with p
\loop\ifnum\n>0 \printifprime\advance\p by2 \repeat}
\def\printp{, % we will invoke \printp if p is prime
\ifnum\n=1 and~\fi % this precedes the last value
\number\p \advance\n by -1 }
\def\printifprime{\testprimality \ifprime\printp\fi}
\def\testprimality{{\d=3 \global\primetrue
\loop\trialdivision \ifunknown\advance\d by2 \repeat}}
\def\trialdivision{\a=\p \divide\a by\d
\ifnum\a>\d \unknowntrue\else\unknownfalse\fi
\multiply\a by\d
\ifnum\a=\p \global\primefalse\unknownfalse\fi}


In this code Knuth uses \number\p.

-
I have read the book first time twenty years ago but this is the first time I am reading the book (I have it next to me) with the mind set that TeX is general programming language. It is overwhelming with the breath of information. He has whole chapter with many exercises on arithmetics. We do not use TeX as a general purpose programming language but I am trying to discover those dark corners of TeX which I never use as a working Mathematician. –  Predrag Punosevac Dec 27 '11 at 13:49