WOLFRAM

Enable JavaScript to interact with content and submit forms on Wolfram websites. Learn how
Wolfram Language & System Documentation Center

NextPrime [x]

gives the smallest prime above x.

NextPrime [x,k]

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

Details
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Numerical Evaluation  
Symbolic Manipulation  
Applications  
Basic Applications  
Approximations  
Number Theory  
Special Sequences  
Properties & Relations  
Neat Examples  
See Also
Tech Notes
Related Guides
History
Cite this Page

NextPrime [x]

gives the smallest prime above x.

NextPrime [x,k]

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

Details

  • 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.

Examples

open all close all

Basic Examples  (2)

Compute the next prime after 10:

Wolfram Language code: NextPrime[10]

Plot the sequence of primes:

Wolfram Language code: Plot[NextPrime[x], {x, 0, 50}]

Scope  (9)

Numerical Evaluation  (7)

NextPrime works over integers:

Wolfram Language code: NextPrime[6]

Rational numbers:

Wolfram Language code: NextPrime[7 / 2]

Real numbers:

Wolfram Language code: NextPrime[100.5]

Compute the last prime before 50:

Wolfram Language code: NextPrime[50, -1]

Compute the second prime after 50:

Wolfram Language code: NextPrime[50, 2]

NextPrime works over large integers:

Wolfram Language code: NextPrime[10 ^ 100]

NextPrime threads over lists:

Wolfram Language code: NextPrime[{1, 5, 10, 15}]

Symbolic Manipulation  (2)

Find a solution instance of equalities with NextPrime :

Wolfram Language code: FindInstance[NextPrime[p] == p + 1 && 0 < p < 10, p, Integers]

Reduce an equation:

Wolfram Language code: Reduce[NextPrime[x] - x == 2 && 0 < x < 10, x∈Integers]

Applications  (15)

Basic Applications  (6)

Highlight the sequence of primes using NextPrime :

Wolfram Language code: Multicolumn[Table[If[NextPrime[n - 1] == n, Framed[Style[n, Red]], n], {n, 1, 70}], ...]

Visualize the primes with a parabolic sieve:

Wolfram Language code: Plot[{x^(1/2), -x^(1/2)}, {x, 0, 10}, Sequence[...]]

Visualize the sieve of Eratosthenes for the first 5 primes:

Wolfram Language code: Plot[Evaluate[Table[NextPrime[n] * Sin[x * Pi / NextPrime[n]], {n, 10}]], {x, 0, 30} ]

Spiral of a NextPrime sequence:

Wolfram Language code: ListPolarPlot[Table[{Mod[π / 2 - n 4 π / 20, 2 π], NextPrime[n]}, {n, 20}], ...]

Hexagonal prime spiral using NextPrime :

Wolfram Language code: hexagonalSpiral[n_] := {Re[#], Im[#]}& /@ Fold[Join[#1, Last[#1] + Exp[I Pi / 3] ^ #2 Range[#2 / 2]]&, {0}, Range[n]]
Wolfram Language code: spiral = hexagonalSpiral[25];
Wolfram Language code: nextprimes = spiral[[DeleteDuplicates[NextPrime[Range[PrimePi[5 * Length[spiral]]]]]]];
Wolfram Language code: ListPlot[spiral, ...]

Find the lowest-divisor pair for an integer :

Wolfram Language code: firstDivisorPair[n_Integer] := If[n == 1, 1, (If[PrimeQ[#[[1]]], #[[1]], #[[1]][#[[2]], Divide@@#]]&)[NestWhile[MapAt[NextPrime, #, 2]&, {n, 2}, ! IntegerQ[Divide@@#]&]]]
Wolfram Language code: firstDivisorPair[24]

Find the prime factorization for an integer :

Wolfram Language code: trialByDivision[n_Integer] := First[FixedPoint[MapAt[firstDivisorPair, #, Last[Position[#, _Integer, Heads -> False]]]&, {n}]]
Wolfram Language code: trialByDivision[24]

Construct a factorization tree for an integer :

Wolfram Language code: factorTree[n_Integer] := TreeForm[ trialByDivision[n], Rule[...]]
Wolfram Language code: factorTree[24]

Approximations  (2)

Approximation to Prime :

Wolfram Language code: NextPrime[1, 25]
Wolfram Language code: 25 * Log[25.]

Plot Prime compared with an estimate:

Wolfram Language code: ListStepPlot[{DeleteDuplicates@Table[NextPrime[n], {n, 95}], Table[n Log[n], {n, 25}]}]

Compute Prime :

Wolfram Language code: DeleteDuplicates[Table[NextPrime[n], {n, 1, 95}]]
Wolfram Language code: Table[Prime[n], {n, 1, 25}]

Number Theory  (5)

NextPrime numbers modulo 10:

Wolfram Language code: Mod[Array[NextPrime, 100], 10]
Wolfram Language code: ListLinePlot[Mod[Array[NextPrime, 100], 10]]

NextPrime numbers modulo :

Wolfram Language code: ListLinePlot[Table[Mod[NextPrime[n], n], {n, 1, 100}]]

Plot prime gaps around powers of 10:

Wolfram Language code: ListLinePlot[Table[NextPrime[10 ^ s] - NextPrime[10 ^ s, -1], {s, 50}]]

Visualize the gaps between primes below 104:

Wolfram Language code: Histogram[Table[NextPrime[s] - NextPrime[s, -1], {s, 10 ^ 4}]]

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

Wolfram Language code: Histogram[Table[Min[{Abs[NextPrime[n, -1]], Abs[NextPrime[n]]}], {n, 1, 10 ^ 4}]]

Compare the NextPrime sequence to PrimePi :

Wolfram Language code: DiscretePlot[{NextPrime[x], PrimePi[x]}, {x, 0, 50}]

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

Wolfram Language code: n = RandomInteger[500]
Wolfram Language code: n < NextPrime[n] < 2n

Verify this for a range of integers:

Wolfram Language code: AllTrue[Table[n < NextPrime[n] < 2n, {n, 2, 200}], #&]

Special Sequences  (2)

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

Wolfram Language code: Select[Range[100], PrimeQ[#] && NextPrime[#] == 2 + #&]

Plot the twin primes:

Wolfram Language code: ListStepPlot[Select[Range[100], PrimeQ[#] && NextPrime[#] == 2 + #&]]

Compute the smallest n-digit prime:

Wolfram Language code: NextPrime[10 ^ #]& /@ Range[0, 20]

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

Wolfram Language code: (NextPrime[10 ^ #] - 10 ^ #)& /@ Range[0, 20]
Wolfram Language code: DiscretePlot[NextPrime[10 ^ n] - 10 ^ n, {n, 0, 50}]
Wolfram Language code: ListStepPlot[(NextPrime[10 ^ #] - 10 ^ #)& /@ Range[0, 50]]

Properties & Relations  (4)

Primes represents the domain of all primes numbers:

Wolfram Language code: Element[NextPrime[30], Primes]

The largest domain of definitions of NextPrime :

Wolfram Language code: FunctionDomain[NextPrime[n, k], n]

Compute the prime using NextPrime :

Wolfram Language code: NextPrime[1, 39]
Wolfram Language code: Prime[39]

Compute using PrimePi :

Wolfram Language code: NextPrime[15]
Wolfram Language code: Prime[1 + PrimePi[15]]

Neat Examples  (5)

Ulam spiral of the distance to the next prime:

Wolfram Language code: ulam[n_] := Partition[Permute[Range[n ^ 2], Accumulate[Take[Flatten[{{n ^ 2 + 1} / 2, Table [(-1) ^ j i, {j, n}, {i, {-1, n}}, {j}]}], n ^ 2]]], n]
Wolfram Language code: ArrayPlot[NextPrime[ulam[101]] - ulam[101], Rule[...]]

Generate a path based on the NextPrime sequence:

Wolfram Language code: Graphics[Line[AnglePath[Table[NextPrime[n], {n, 1, 100}]], Rule[...]]]

Visualize the increase in prime numbers through -sided polygons:

Wolfram Language code: Table[Graphics[{EdgeForm[StandardGray], Riffle[RandomColor@50, Polygon /@ (CirclePoints[#, 1, NextPrime[n]]& /@ Tuples[Range@1, {2}])]}], {n, 2, 5}]

Plot NextPrime using ArrayPlot :

Wolfram Language code: ArrayPlot[((Outer[Plus, #, #]&)[NextPrime[Range[2, 20]]] - Outer[NextPrime[+##1]&, #, #]&)[Range[2, 20]], Rule[...]]

Sunflower seed representation of NextPrime :

Wolfram Language code: ListPlot[Table[Sqrt[a] {Cos[((2 * Pi) * a) / GoldenRatio ^ 2], Sin[((2 * Pi) * a) / GoldenRatio ^ 2]}, {a, NextPrime@Range@5000}], ...]

See Also

Prime   PrimePi   RandomPrime   Primes   PrimeQ   CompositeQ   PrimePowerQ   FactorInteger

Function Repository: ClosestPrime   TwinPrimes

Wolfram Research (2007), NextPrime, Wolfram Language function, https://reference.wolfram.com/language/ref/NextPrime.html (updated 2020).

Text

Wolfram Research (2007), NextPrime, Wolfram Language function, https://reference.wolfram.com/language/ref/NextPrime.html (updated 2020).

CMS

Wolfram Language. 2007. "NextPrime." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2020. https://reference.wolfram.com/language/ref/NextPrime.html.

APA

Wolfram Language. (2007). NextPrime. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NextPrime.html

BibTeX

@misc{reference.wolfram_2026_nextprime, author="Wolfram Research", title="{NextPrime}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/NextPrime.html}", note=[Accessed: 12-June-2026]}

BibLaTeX

@online{reference.wolfram_2026_nextprime, organization={Wolfram Research}, title={NextPrime}, year={2020}, url={https://reference.wolfram.com/language/ref/NextPrime.html}, note=[Accessed: 12-June-2026]}

Top [フレーム]

AltStyle によって変換されたページ (->オリジナル) /