gives the smallest prime above x.


gives the k^(th)-next prime above x.


  • When used with a negative k value, NextPrime will give the previous k^(th)prime.
  • Mathematical function, suitable for both symbolic and numerical manipulation.
  • For positive x, NextPrime[x] is the (m+1)^(th) prime number where m is the number of primes less than or equal to x.
  • NextPrime[x, k] is the (m+k)^(th) prime number where m is the number of primes less than or equal to x.


open allclose all

Basic Examples  (2)

Compute the next prime after 10:

Plot the sequence of primes:

Scope  (9)

Numerical Evaluation  (7)

NextPrime works over integers:

Rational numbers:

Real numbers:

Compute the last prime before 50:

Compute the second prime after 50:

NextPrime works over large integers:

NextPrime threads over lists:

Symbolic Manipulation  (2)

Find a solution instance of equalities with NextPrime:

Reduce an equation:

Applications  (15)

Basic Applications  (6)

Highlight the sequence of primes using NextPrime:

Visualize the primes with a parabolic sieve:

Visualize the sieve of Eratosthenes for the first 5 primes:

Spiral of a NextPrime sequence:

Hexagonal prime spiral using NextPrime:

Find the lowest-divisor pair for an integer :

Find the prime factorization for an integer :

Construct a factorization tree for an integer :

Approximations  (2)

Approximation to Prime:

Plot Prime compared with an estimate:

Compute Prime:

Number Theory  (5)

NextPrime numbers modulo 10:

NextPrime numbers modulo :

Plot prime gaps around powers of 10:

Visualize the gaps between primes below 104:

Plot a histogram of the closest distances to a prime for a range of integers:

Compare the NextPrime sequence to PrimePi:

It is conjectured that for any integer , there is a prime with :

Verify this for a range of integers:

Special Sequences  (2)

Find twin primes, i.e. pairs of primes of the form :

Plot the twin primes:

Compute the smallest n-digit prime:

Plot the difference between the smallest n-digit prime and :

Properties & Relations  (4)

Primes represents the domain of all primes numbers:

The largest domain of definitions of NextPrime:

Compute the prime using NextPrime:

Compute using PrimePi:

Neat Examples  (5)

Ulam spiral of the distance to the next prime:

Generate a path based on the NextPrime sequence:

Visualize the increase in prime numbers through -sided polygons:

Plot NextPrime using ArrayPlot:

Sunflower seed representation of NextPrime:

Wolfram Research (2007), NextPrime, Wolfram Language function, (updated 2020).


Wolfram Research (2007), NextPrime, Wolfram Language function, (updated 2020).


Wolfram Language. 2007. "NextPrime." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020.


Wolfram Language. (2007). NextPrime. Wolfram Language & System Documentation Center. Retrieved from


@misc{reference.wolfram_2024_nextprime, author="Wolfram Research", title="{NextPrime}", year="2020", howpublished="\url{}", note=[Accessed: 16-June-2024 ]}


@online{reference.wolfram_2024_nextprime, organization={Wolfram Research}, title={NextPrime}, year={2020}, url={}, note=[Accessed: 16-June-2024 ]}