# Page Numbering: Prime Numbers

I'm writing a project and would like each of my pages be numbered in terms of primes. That is, rather than 1,2,3,..., I'd like 2,3,5,.... I managed to find the following three pages:

However, not being tech savvy, I have no idea how to use this. Just in case it makes a difference, I already know I need 49 pages and so I only need the first 49 primes (in case this let you define a set of some sort that it can utilise). Is there a way to do this without having to reset the counter on each page (which would require knowing where each page begins)?

• I believe you mean "However, not being TeX savvy..." :) – Paul H Mar 11 '14 at 21:57
• Indeed I do. :P – user60126 Mar 11 '14 at 23:02
• Have you just read The Curious Incident... by Mark Haddon? :) – Steve D Mar 18 '14 at 16:51
• Believe it or not, my mother was an English teacher and, being unable to relate to mathematics in any way despite the fact I keep talking to her about it, she keeps using this book as an example. :P It was also that book that gave me the idea to number it in primes. So, yes. :P Amazing book. – user60126 Mar 19 '14 at 17:57
• For an added bonus why not do it in Mersenne Primes...? It might get tricky if your book is longer than 48 pages... :) – McGafter Sep 19 '14 at 13:13

\documentclass{article}
\newcommand{\nthprime}[1]{%
\ifcase #1\relax
\or 2\or 3\or 5\or 7\or 11\or 13\or 17\or 19\or 23\or 29\or 31\or
37\or 41\or 43\or 47\or 53\or 59\or 61\or 67\or 71\or 73\or 79\or
83\or 89\or 97\or 101\or 103\or 107\or 109\or 113\or 127\or 131\or
137\or 139\or 149\or 151\or 157\or 163\or 167\or 173\or 179\or 181\or
191\or 193\or 197\or 199\or 211\or 223\or 227\or 229\or 233\or 239\or
241\or 251\else 997\fi }
\renewcommand{\thepage}{\nthprime{\value{page}}}
\usepackage[paperheight=6cm]{geometry}
\usepackage{lipsum}
\begin{document}
\tableofcontents
\newcount\cnta
\cnta1
\loop
\section{a section}
\lipsum[\cnta-\numexpr\cnta+3\relax]
\ifnum\cnta < 50
\repeat
\end{document}

• What??!! You're not going to calculate the primes on the fly?? ;^) – Steven B. Segletes Mar 11 '14 at 20:07
• @StevenB.Segletes you made me laugh. ;-) – user4686 Mar 11 '14 at 20:09
• @StevenB.Segletes the reason was due to an hesitation to again use xint in an answer here, or rather xinttools as only short integers are needed, and without it I feel unarmed... – user4686 Mar 11 '14 at 20:12
• If you change your mind, various answers at tex.stackexchange.com/questions/134305/… might help. – Steven B. Segletes Mar 11 '14 at 20:13
• @user60126 you are welcome! hope your readers will know the list by heart then. Although you will have to inform them it never stops ;-). – user4686 Mar 13 '14 at 22:40

Today I have already answered a similar code-golf question "Output the first 200 primes using TeX".

My answer needs 176 bytes to print the first 200 prime numbers with plain TeX. I have ungolfed the code and adapted it to LaTeX. As interface \primenumber/\primecounter are added as equivalents to \@alph/\alph. Internally the prime numbers are stored in macros \prime@<number>.

\documentclass{article}

\makeatletter
\begingroup
\countdef\i=1 %
\countdef\j=2 %
\countdef\k=3 %
\countdef\ii=4 %
\newcount\prime@count
\prime@count=1 %
\global\@namedef{prime@1}{2}%
\i=3
\loop
\ifnum\prime@count<200 %
{% group with curly braces because of the nested \loop
% Test for all 2 <= j < i: trunc(i/j)*j == i
% If true, then j is divisor of i and i is not prime
\j=3
% small optimization, \ii should be sqrt(i),
% it is approximated by \ii/3 + 1
\ii=\i
\divide\ii 3
\let\x.%
\loop
\ifnum\j<\ii
\k=\i
\divide\k \j
\multiply\k \j
\ifnum\k=\i
\let\iterate\@empty
\let\x!%
\fi
\repeat
\ifx\x.%
\expandafter\xdef\csname prime@\the\prime@count\endcsname{\the\i}%
\fi
}%
\advance\i\tw@ % test next odd number
\repeat
\endgroup

\@ifundefined{prime@\number#1}\@ctrerr{\csname prime@\number#1\endcsname}%
}
\newcommand*{\primecounter}[1]{%
}
\makeatother

\begin{document}

\newcounter{test}
\renewcommand*{\thetest}{\primecounter{test}}

\setcounter{test}{1}
\makeatletter
\@whilenum\value{test}<\prime@count\do{%
\thetest, %
\stepcounter{test}%
}%
\makeatother

\end{document}


You can find a precompiled list of 10000 prime numbers, so there's no need to compute them.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\PRIME}{m}
{
\seq_item:Nn \g_prime_list_seq { \int_use:c { c@#1 } }
}

\seq_new:N \g_prime_list_seq
\group_begin:
\char_set_catcode_space:n  {\ }
\seq_gset_split:Nnn \g_prime_list_seq { ~ }
{% a longer list is found at http://primes.utm.edu/lists/small/10000.txt
2      3      5      7     11     13     17     19     23     29
31     37     41     43     47     53     59     61     67     71
73     79     83     89     97    101    103    107    109    113
127    131    137    139    149    151    157    163    167    173
179    181    191    193    197    199    211    223    227    229
233    239    241    251    257    263    269    271    277    281
283    293    307    311    313    317    331    337    347    349
353    359    367    373    379    383    389    397    401    409
419    421    431    433    439    443    449    457    461    463
467    479    487    491    499    503    509    521    523    541
547    557    563    569    571    577    587    593    599    601
607    613    617    619    631    641    643    647    653    659
661    673    677    683    691    701    709    719    727    733
739    743    751    757    761    769    773    787    797    809
811    821    823    827    829    839    853    857    859    863
877    881    883    887    907    911    919    929    937    941
947    953    967    971    977    983    991    997   1009   1013
1019   1021   1031   1033   1039   1049   1051   1061   1063   1069
1087   1091   1093   1097   1103   1109   1117   1123   1129   1151
1153   1163   1171   1181   1187   1193   1201   1213   1217   1223
1229   1231   1237   1249   1259   1277   1279   1283   1289   1291
1297   1301   1303   1307   1319   1321   1327   1361   1367   1373
1381   1399   1409   1423   1427   1429   1433   1439   1447   1451
1453   1459   1471   1481   1483   1487   1489   1493   1499   1511
1523   1531   1543   1549   1553   1559   1567   1571   1579   1583
1597   1601   1607   1609   1613   1619   1621   1627   1637   1657
1663   1667   1669   1693   1697   1699   1709   1721   1723   1733
1741   1747   1753   1759   1777   1783   1787   1789   1801   1811
}
\group_end:
\ExplSyntaxOn

\renewcommand{\thepage}{\PRIME{page}}

\begin{document}

\tableofcontents

\ExplSyntaxOn % just to fill contents
\prg_replicate:nn { 30 } { \section{A} x \newpage }
\ExplSyntaxOff

\end{document}
`

• why do just what we need? what about fun? – David Carlisle Mar 11 '14 at 23:46