Here is a never ending counting of the primes... (except that it will end-up in some arithmetic overflow error after a while).
For less overhead, I use plain PDFTeX. Launching the run in a Terminal, the page numbers correspond to how many primes have been computed so far.
update: the algorithm was a bit faulty, although it computed correctly the primes. The tested integer n
had to receive from time
to time a +2
kick that was missing, so that the amount of divisions
done slowly drifted above the necessary. Furthermore my imprudent use
of \1
,\2
, .. gave unwanted space tokens in the output (this was
very silly on my part, but I have learned my lesson).
..........(terminal output until ^C interrupt)...........
[150451] [150452] [150453] [150454] [150455] [150456] [150457] [150458]
[150459] [150460] [150461] [150462] [150463] [150464] [150465] [150466]
[150467] [150468] [150469] [150470] [150471]^C
! Interruption.
<to be read again>
\TestDiv
\TestDiv ...expandafter \expandafter \TestDiv \fi
\fi
\ToLogIfPrime ->\e \1 \TestDiv
\ifnum \e >\k \write \mone {\the \n }\the \n ...
\CheckNs ...m \j <\k \advance \n \2 \ToLogIfPrime
\advance \j \1 \expandafte...
\FindPrimes ->\j \0 \CheckNs
\advance \k \1 \FindPrimes
l.73 \FindPrimes
? X
</usr/local/texlive/2012/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
Output written on primes.pdf (150471 pages, 28821382 bytes).
Transcript written on primes.log.
The file primes.pdf
is not corrupted here are the last pages for this run:

Ah, and some confirmation:
|\^/| Maple 12 (IBM INTEL LINUX)
._|\| |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2008
\ MAPLE / All rights reserved. Maple is a trademark of
<____ ____> Waterloo Maple Inc.
| Type ? for help.
> ithprime (150471);
2021959
Also, the code writes the primes into the log file: (I don't know the origin of some additional linebreaks in the log file, like here just above page number 150467)
] [150461
2021807
] [150462
2021837
] [150463
2021839
] [150464
2021843
] [150465
2021849
] [150466
2021863
]
[150467
2021879
] [150468
2021891
] [150469
2021927
] [150470
2021933
] [150471
2021959
]
! Interruption.
Here is the code (just the basic algorithm of testing for divisors until square root of n, without adornments; and minus an unfortunate oversight of the previous version, as explained above):
% Time-stamp: <30-01-2013 19:19:16 CET jfbu>
% file primes.tex to find and count prime numbers
% page geometry
\pdfoutput = 1
\hoffset = -2.44cm
\voffset = -2.44cm
\pdfpagewidth = 7cm
\pdfpageheight = .7cm
\hsize 6.8cm
\vsize .5cm
\parindent 0pt
\footline {}
\chardef\zero 0
\chardef\one 1
\chardef\two 2
\newcount\mone
\mone = -1
\newcount\n
\newcount\m
\newcount\e
\newcount\k
\newcount\j
\def\TestDiv{%
\advance\e\two
\ifnum\e>\k\else
\m\n
\divide\m\e
\multiply\m\e
\ifnum\m=\n \else
\expandafter\expandafter\expandafter
\TestDiv
\fi
\fi}
\def\ToLogIfPrime{\e\one
\TestDiv
\ifnum\e>\k
% \write\sixteen{\the\n}\leavevmode\vfill\eject
\write\mone{\the\n}\the\n\ (\the\count\zero)\vfill\eject
\fi}
\def\CheckNs{%
\ifnum\j<\k
\advance\n\two \ToLogIfPrime
\advance\j\one
\expandafter\CheckNs
\fi}
\def\FindPrimes{%
\j\zero % k is even,
\CheckNs % we scan the k odd n's such that k^2 < n < (k+1)^2
\advance\k\one % now k is odd and the next odd n is k^2
\advance\n\two % which certainly is not prime.
\j\zero
\CheckNs % from k^2+2 up to (k+1)^2 - 1, this makes k n's to go
%\ifnum\k<100 % <- set this if you want finite computation
\advance\k\one
%\expandafter
\FindPrimes
%\fi
}
\k 2
\n 3
\write-1{2}2 (1)\vfill\eject
\write-1{3}3 (2)\vfill\eject
%
\FindPrimes
\bye
\def\foo{\foo}\foo
? – cgnieder Jan 29 '13 at 0:38\count255=0 \loop\ifnum\count255>0 Print something\endgraf\repeat
– egreg Jan 29 '13 at 10:44forever
primitive loop. – egreg Jan 30 '13 at 10:50