primes Table Function
-
primes()– Returns an infinite table with a singleprimecolumn (UInt64) that contains prime numbers in ascending order, starting from 2. UseLIMIT(and optionallyOFFSET) to restrict the number of rows. -
primes(N)– Returns a table with the singleprimecolumn (UInt64) that contains the firstNprime numbers, starting from 2. -
primes(N, M)– Returns a table with the singleprimecolumn (UInt64) that containsMprime numbers starting at theN-th prime (0-based). -
primes(N, M, S)– Returns a table with the singleprimecolumn (UInt64) that containsMprime numbers starting from theN-th prime (0-based) with stepSby prime index. The returned primes correspond to indicesN, N + S, N + 2S, ..., N + (M - 1)S.Smust be ≥ 1.
This is similar to the system.primes system table.
The following queries are equivalent:
The following queries are also equivalent:
Examples
The first 10 primes.
The first prime greater than 1e15.
The first 7 Mersenne primes.
Notes
- The fastest forms are the plain range and point-filter queries that use the default step (
1), for example,primes(N)orprimes() LIMIT N. These forms use an optimized prime generator to compute very large primes efficiently. For example, the following query executes almost instantly:
- Using a non-zero offset and/or step greater than 1 (
primes(offset, count)/primes(offset, count, step)) may be slower because additional primes may need to be generated and skipped internally. If you don't need an offset or step, omit them.