Tx · LaTeX & TikZ

How to Produce a List of Prime Numbers in LaTeX

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose a format

    Use an itemized list for a vertical sequence, or a tabular environment to arrange primes in rows and columns.

  2. 2. Enter the primes

    LaTeX formats the numbers; it does not automatically determine which integers are prime.

  3. 3. Compile and check

    Compile the example and verify the range so no prime is missing and no composite number is included.

LaTeX can typeset a list of prime numbers using ordinary list or table environments. For a compact reference, a table is often easiest to scan; for a short sequence in a paragraph-like layout, an itemized list works well.

The examples below show primes up to 100 in a five-column table and a vertical list. LaTeX formats the numbers you enter, but does not automatically test whether they are prime.

Put primes in a compact table

Use tabular when you want the numbers arranged in rows and columns. This complete example compiles with a standard LaTeX installation and needs no extra packages:

\documentclass{article}

\begin{document}

\begin{center}
\begin{tabular}{*{5}{c}}
  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 \\
\end{tabular}
\end{center}

\end{document}

The column specification *{5}{c} means “repeat a centered column five times.” Each & moves to the next column, and \\ ends the current row. The center environment centers the whole table on the page; it does not change the alignment of values inside each cell.

The example lists all 25 primes not exceeding 100. If you prefer, add horizontal rules with \hline after the column specification and between rows. For many documents, though, a plain table without vertical or horizontal grid lines is easier to read.

Use a vertical list instead

For a short list, itemize is straightforward:

\begin{itemize}
  \item 2
  \item 3
  \item 5
  \item 7
  \item 11
\end{itemize}

This produces a bulleted list. Use enumerate instead if the entries need item numbers. If you want a single comma-separated sequence in running text, write the values directly, such as 2, 3, 5, 7, 11.

Does LaTeX calculate prime numbers?

Not in the examples above. LaTeX is a typesetting system, so it will print a composite number just as readily as a prime one. You are responsible for supplying the correct values. This is usually fine for a short, fixed list.

For a long list or one that changes often, generate the primes with a program and paste the output into your LaTeX source. For example, this Python function uses trial division to find primes up to a chosen limit:

from math import isqrt

def primes_up_to(limit):
    primes = []
    for n in range(2, limit + 1):
        if all(n % d != 0 for d in range(2, isqrt(n) + 1)):
            primes.append(n)
    return primes

print(primes_up_to(30))

The output is [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]. You can then format those values as 2 & 3 & 5 & ... for a table, or place each value in an \item. For very large limits, use a more efficient prime-generation algorithm than trial division.

Common mistakes

  • Putting & outside a table: In a tabular environment, & separates cells. Outside an alignment environment it can cause a “Misplaced alignment tab character” error. For a literal ampersand in prose, use \&.
  • Forgetting to end a row: Use \\ between table rows. A single backslash is not the row-ending command.
  • Including 1 as a prime: By the standard definition, a prime is an integer greater than 1 with exactly two positive divisors. Therefore, 1 is not prime.
  • Mixing list formats: Use \item only inside a list environment such as itemize or enumerate. For a grid, use the & and \\ separators inside tabular.
  • Assuming the table checks its contents: A table only controls layout. It will not flag missing primes or detect composites, so verify generated or hand-entered values before compiling.

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.