NextPrime
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 allBasic Examples (2)
Compute the next prime after 10:
NextPrime[10]Plot the sequence of primes:
Plot[NextPrime[x], {x, 0, 50}]Scope (9)
Numerical Evaluation (7)
NextPrime works over integers:
NextPrime[6]Rational numbers:
NextPrime[7 / 2]Real numbers:
NextPrime[100.5]Compute the last prime before 50:
NextPrime[50, -1]Compute the second prime after 50:
NextPrime[50, 2]NextPrime works over large integers:
NextPrime[10 ^ 100]NextPrime threads over lists:
NextPrime[{1, 5, 10, 15}]Symbolic Manipulation (2)
Find a solution instance of equalities with NextPrime :
FindInstance[NextPrime[p] == p + 1 && 0 < p < 10, p, Integers]Reduce an equation:
Reduce[NextPrime[x] - x == 2 && 0 < x < 10, x∈Integers]Applications (15)
Basic Applications (6)
Highlight the sequence of primes using NextPrime :
Multicolumn[Table[If[NextPrime[n - 1] == n, Framed[Style[n, Red]], n], {n, 1, 70}], ...]Visualize the primes with a parabolic sieve:
Plot[{x^(1/2), -x^(1/2)}, {x, 0, 10}, Sequence[...]]Visualize the sieve of Eratosthenes for the first 5 primes:
Plot[Evaluate[Table[NextPrime[n] * Sin[x * Pi / NextPrime[n]], {n, 10}]], {x, 0, 30} ]Spiral of a NextPrime sequence:
ListPolarPlot[Table[{Mod[π / 2 - n 4 π / 20, 2 π], NextPrime[n]}, {n, 20}], ...]Hexagonal prime spiral using NextPrime :
hexagonalSpiral[n_] := {Re[#], Im[#]}& /@ Fold[Join[#1, Last[#1] + Exp[I Pi / 3] ^ #2 Range[#2 / 2]]&, {0}, Range[n]]spiral = hexagonalSpiral[25];nextprimes = spiral[[DeleteDuplicates[NextPrime[Range[PrimePi[5 * Length[spiral]]]]]]];ListPlot[spiral, ...]Find the lowest-divisor pair for an integer :
firstDivisorPair[n_Integer] :=
If[n == 1, 1, (If[PrimeQ[#[[1]]], #[[1]], #[[1]][#[[2]], Divide@@#]]&)[NestWhile[MapAt[NextPrime, #, 2]&, {n, 2}, ! IntegerQ[Divide@@#]&]]]firstDivisorPair[24]Find the prime factorization for an integer :
trialByDivision[n_Integer] :=
First[FixedPoint[MapAt[firstDivisorPair, #, Last[Position[#, _Integer, Heads -> False]]]&, {n}]]trialByDivision[24]Construct a factorization tree for an integer :
factorTree[n_Integer] :=
TreeForm[ trialByDivision[n], Rule[...]]factorTree[24]Approximations (2)
Approximation to Prime :
NextPrime[1, 25]25 * Log[25.]Plot Prime compared with an estimate:
ListStepPlot[{DeleteDuplicates@Table[NextPrime[n], {n, 95}], Table[n Log[n], {n, 25}]}]Compute Prime :
DeleteDuplicates[Table[NextPrime[n], {n, 1, 95}]]Table[Prime[n], {n, 1, 25}]Number Theory (5)
NextPrime numbers modulo 10:
Mod[Array[NextPrime, 100], 10]ListLinePlot[Mod[Array[NextPrime, 100], 10]]NextPrime numbers modulo :
ListLinePlot[Table[Mod[NextPrime[n], n], {n, 1, 100}]]Plot prime gaps around powers of 10:
ListLinePlot[Table[NextPrime[10 ^ s] - NextPrime[10 ^ s, -1], {s, 50}]]Visualize the gaps between primes below 104:
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:
Histogram[Table[Min[{Abs[NextPrime[n, -1]], Abs[NextPrime[n]]}], {n, 1, 10 ^ 4}]]Compare the NextPrime sequence to PrimePi :
DiscretePlot[{NextPrime[x], PrimePi[x]}, {x, 0, 50}]It is conjectured that for any integer , there is a prime with :
n = RandomInteger[500]n < NextPrime[n] < 2nVerify this for a range of integers:
AllTrue[Table[n < NextPrime[n] < 2n, {n, 2, 200}], #&]Special Sequences (2)
Find twin primes, i.e. pairs of primes of the form :
Select[Range[100], PrimeQ[#] && NextPrime[#] == 2 + #&]Plot the twin primes:
ListStepPlot[Select[Range[100], PrimeQ[#] && NextPrime[#] == 2 + #&]]Compute the smallest n-digit prime:
NextPrime[10 ^ #]& /@ Range[0, 20]Plot the difference between the smallest n-digit prime and :
(NextPrime[10 ^ #] - 10 ^ #)& /@ Range[0, 20]DiscretePlot[NextPrime[10 ^ n] - 10 ^ n, {n, 0, 50}]ListStepPlot[(NextPrime[10 ^ #] - 10 ^ #)& /@ Range[0, 50]]Properties & Relations (4)
Primes represents the domain of all primes numbers:
Element[NextPrime[30], Primes]The largest domain of definitions of NextPrime :
FunctionDomain[NextPrime[n, k], n]Compute the prime using NextPrime :
NextPrime[1, 39]Prime[39]Compute using PrimePi :
NextPrime[15]Prime[1 + PrimePi[15]]Neat Examples (5)
Ulam spiral of the distance to the next prime:
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]ArrayPlot[NextPrime[ulam[101]] - ulam[101], Rule[...]]Generate a path based on the NextPrime sequence:
Graphics[Line[AnglePath[Table[NextPrime[n], {n, 1, 100}]], Rule[...]]]Visualize the increase in prime numbers through -sided polygons:
Table[Graphics[{EdgeForm[StandardGray], Riffle[RandomColor@50, Polygon /@ (CirclePoints[#, 1, NextPrime[n]]& /@ Tuples[Range@1, {2}])]}], {n, 2, 5}]Plot NextPrime using ArrayPlot :
ArrayPlot[((Outer[Plus, #, #]&)[NextPrime[Range[2, 20]]] - Outer[NextPrime[+##1]&, #, #]&)[Range[2, 20]], Rule[...]]Sunflower seed representation of NextPrime :
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
Tech Notes
Related Guides
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]}