Skip to content

NthPrime ​

The nth prime number (NthPrime(1) = 2).

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaPrime numberMathWorldPrimeNumberWikidataQ49008Encyclopedia of MathematicsPrime_numbernLabprime numberBritannicatopic/prime-numberOEISA000040Wolfram LanguagePrime✓
NthPrime(n)the nnth prime, with NthPrime(1)=2\mathrm{NthPrime}(1)=2.
SageMathnth_prime(n)✓SymPyprime(n)Rustnth_prime(n)
Typed signatures
TypePackageApplies
(integer) -> integercompute-enginealways
(integer) -> integernumber-theoryreplaces compute-enginealways

Domain: Number theory

Details
  • Inverse of PrimePi: π(pn)=n\pi(p_n)=n.
  • Grows asymptotically like nln⁡nn\ln n, by the prime number theorem.
  • n must be a positive integer; compute-engine leaves non-positive n, including NthPrime(0), unevaluated.
  • Threads element-wise over a list, as Wolfram's Listable heads do.

Plot

Examples

Basic #

The first prime is 2

live ↗
InNthPrime(1)\mathrm{NthPrime}(1)Out22
live ↗
InNthPrime(10)\mathrm{NthPrime}(10)Out2929
live ↗
InNthPrime(100)\mathrm{NthPrime}(100)Out541541
Scope #

Threads element-wise over a list, as Wolfram's Listable heads do

live ↗
InNthPrime([1,3,4,10])\mathrm{NthPrime}(\bigl\lbrack1, 3, 4, 10\bigr\rbrack)Out[2,5,7,29]\bigl\lbrack2, 5, 7, 29\bigr\rbrack

The millionth prime

live ↗
InNthPrime(1 000 000)\mathrm{NthPrime}(1\,000\,000)Out15 485 86315\,485\,863
Applications #

A prime gap: p31−p30=127−113p_{31}-p_{30}=127-113

live ↗
InNthPrime(31)−NthPrime(30)\mathrm{NthPrime}(31)-\mathrm{NthPrime}(30)Out1414

The 25th prime is the last one below 100

live ↗
InNthPrime(25)\mathrm{NthPrime}(25)Out9797

Last digits of the first 100 primes; only 1, 3, 7, 9 after the first two

live ↗
InArray(NthPrime,100) mod 10\mathrm{Array}(\mathrm{NthPrime}, 100)\bmod10Out[2,3,5,7,1,3,7,9,3,9,1,7,1,3,7,3,9,1,7,1,3,9,3,9,7,1,3,7,9,3,7,1,7,9,9,1,7,3,7,3,9,1,1,3,7,9,1,3,7,9,3,9,1,1,7,3,9,1,7,1,3,3,7,1,3,7,1,7,7,9,3,9,7,3,9,3,9,7,1,9,9,1,1,3,9,3,9,7,1,3,7,9,7,1,9,3,9,1,3,1]\bigl\lbrack2, 3, 5, 7, 1, 3, 7, 9, 3, 9, 1, 7, 1, 3, 7, 3, 9, 1, 7, 1, 3, 9, 3, 9, 7, 1, 3, 7, 9, 3, 7, 1, 7, 9, 9, 1, 7, 3, 7, 3, 9, 1, 1, 3, 7, 9, 1, 3, 7, 9, 3, 9, 1, 1, 7, 3, 9, 1, 7, 1, 3, 3, 7, 1, 3, 7, 1, 7, 7, 9, 3, 9, 7, 3, 9, 3, 9, 7, 1, 9, 9, 1, 1, 3, 9, 3, 9, 7, 1, 3, 7, 9, 7, 1, 9, 3, 9, 1, 3, 1\bigr\rbrack

Mertens' product ∏(1−1/p)\prod(1-1/p) over 25 primes, near e−γ/ln⁡pe^{-\gamma}/\ln p

live ↗
InN(100Last(FoldList(Multiply,1,1−1NthPrime(1..25))))N(100\mathrm{Last}(\mathrm{FoldList}(\mathrm{Multiply}, 1, 1-\frac{1}{\mathrm{NthPrime}(1..25)})))Out12.031 729 047 493 518 845 046 377 749 916 002 447 517 547 079 907 308 611 992 957 558 959 774 039 467 813 931 634 754 337 362 993 103 663 911 632 323 741 142 560 525 368 787 535 602 215 437 513 280 363 740 373 267 063 423 780 504 807 034 048 741 363 892 815 775 512 794 985 020 245 119 743 682 156 856 905 745 149 365 745 047 058 318 405 826 205 440 410 170 440 671 667 429 458 821 773 900 960 139 332 334 824 297 814 727 606 130 865 211 851 808 520 213 524 696 348 571 409 991 875 632 191 069 982 658 715 093 864 363 704 286 287 076 786 697 026 702 666 817 476 899 380 475 305 246 364 654 764 856 705 150 394 009 025 529 594 469 766 357 381 674 148 101 642 444 812.031\,729\,047\,493\,518\,845\,046\,377\,749\,916\,002\,447\,517\,547\,079\,907\,308\,611\,992\,957\,558\,959\,774\,039\,467\,813\,931\,634\,754\,337\,362\,993\,103\,663\,911\,632\,323\,741\,142\,560\,525\,368\,787\,535\,602\,215\,437\,513\,280\,363\,740\,373\,267\,063\,423\,780\,504\,807\,034\,048\,741\,363\,892\,815\,775\,512\,794\,985\,020\,245\,119\,743\,682\,156\,856\,905\,745\,149\,365\,745\,047\,058\,318\,405\,826\,205\,440\,410\,170\,440\,671\,667\,429\,458\,821\,773\,900\,960\,139\,332\,334\,824\,297\,814\,727\,606\,130\,865\,211\,851\,808\,520\,213\,524\,696\,348\,571\,409\,991\,875\,632\,191\,069\,982\,658\,715\,093\,864\,363\,704\,286\,287\,076\,786\,697\,026\,702\,666\,817\,476\,899\,380\,475\,305\,246\,364\,654\,764\,856\,705\,150\,394\,009\,025\,529\,594\,469\,766\,357\,381\,674\,148\,101\,642\,444\,8

The fourth prime, 7, is a Mersenne prime exponent

live ↗
InIsMersennePrimeExponent(NthPrime(4))\mathrm{IsMersennePrimeExponent}(\mathrm{NthPrime}(4))OutTrue\mathrm{True}

27−1=1272^7-1=127 is prime

live ↗
InIsPrime(2NthPrime(4)−1)\mathrm{IsPrime}(2^{\mathrm{NthPrime}(4)}-1)OutTrue\mathrm{True}
live ↗
In1∏n=16(1−NthPrime(n)−s)\frac{1}{\prod_{n=1}^6(1-\mathrm{NthPrime}(n)^{-s})}Out1(1−2−s)(1−3−s)(1−5−s)(1−7−s)(1−11−s)(1−13−s)\frac{1}{(1-2^{-s})(1-3^{-s})(1-5^{-s})(1-7^{-s})(1-11^{-s})(1-13^{-s})}
Properties #

PrimePi and NthPrime are inverses: π(pn)=n\pi(p_n)=n

live ↗
InPrimePi(NthPrime(100))=100\mathrm{PrimePi}(\mathrm{NthPrime}(100))=100OutTrue\mathrm{True}

PrimePi inverts NthPrime

live ↗
InPrimePi(NthPrime(100))\mathrm{PrimePi}(\mathrm{NthPrime}(100))Out100100

The next prime after 30 is pπ(30)+1p_{\pi(30)+1}

live ↗
InNthPrime(1+PrimePi(30))\mathrm{NthPrime}(1+\mathrm{PrimePi}(30))Out3131

A prime has exactly one prime factor

live ↗
InPrimeOmega(NthPrime(5))\mathrm{PrimeOmega}(\mathrm{NthPrime}(5))Out11

A prime's only divisors are 1 and itself

live ↗
InDivisors(NthPrime(5))\mathrm{Divisors}(\mathrm{NthPrime}(5))Out[1,11]\bigl\lbrack1, 11\bigr\rbrack

φ(p)=p−1\varphi(p)=p-1

live ↗
InTotient(NthPrime(10))\mathrm{Totient}(\mathrm{NthPrime}(10))Out2828

The only even prime is p1=2p_1=2, so p2p_2 is the smallest odd prime

live ↗
InNthPrime(2)\mathrm{NthPrime}(2)Out33

Known value · Wolfram MathematicalFunctionData, Prime

Primes begin 2, 3, 5

live ↗
InNthPrime(3)\mathrm{NthPrime}(3)Out55

Known value · Wolfram MathematicalFunctionData, Prime

The fourth prime

live ↗
InNthPrime(4)\mathrm{NthPrime}(4)Out77

Known value · Wolfram MathematicalFunctionData, Prime

The fifth prime

live ↗
InNthPrime(5)\mathrm{NthPrime}(5)Out1111

Known value · Wolfram MathematicalFunctionData, Prime

The sixth prime

live ↗
InNthPrime(6)\mathrm{NthPrime}(6)Out1313

Known value · Wolfram MathematicalFunctionData, Prime

Seventh prime, after a gap of 4

live ↗
InNthPrime(7)\mathrm{NthPrime}(7)Out1717

Known value · Wolfram MathematicalFunctionData, Prime

Output is always a member of the primes

live ↗
InNthPrime(30)∈P\mathrm{NthPrime}(30)∈\mathbb{P}OutTrue\mathrm{True}

One less than p10=29p_{10}=29 equals φ(29)\varphi(29)

live ↗
InNthPrime(10)−1\mathrm{NthPrime}(10)-1Out2828

The eighth prime, a twin with 17

live ↗
InNthPrime(8)\mathrm{NthPrime}(8)Out1919

Known value · Wolfram MathematicalFunctionData, Prime

The ninth prime

live ↗
InNthPrime(9)\mathrm{NthPrime}(9)Out2323

Known value · Wolfram MathematicalFunctionData, Prime

Starts the twenties-to-thirties stretch

live ↗
InNthPrime(11)\mathrm{NthPrime}(11)Out3131

Known value · Wolfram MathematicalFunctionData, Prime

The twelfth prime

live ↗
InNthPrime(12)\mathrm{NthPrime}(12)Out3737

Known value · Wolfram MathematicalFunctionData, Prime

The thirteenth prime

live ↗
InNthPrime(13)\mathrm{NthPrime}(13)Out4141

Known value · Wolfram MathematicalFunctionData, Prime

Twin to 41

live ↗
InNthPrime(14)\mathrm{NthPrime}(14)Out4343

Known value · Wolfram MathematicalFunctionData, Prime

The fifteenth prime

live ↗
InNthPrime(15)\mathrm{NthPrime}(15)Out4747

Known value · Wolfram MathematicalFunctionData, Prime

The sixteenth prime

live ↗
InNthPrime(16)\mathrm{NthPrime}(16)Out5353

Known value · Wolfram MathematicalFunctionData, Prime

Twin to 61

live ↗
InNthPrime(17)\mathrm{NthPrime}(17)Out5959

Known value · Wolfram MathematicalFunctionData, Prime

The eighteenth prime

live ↗
InNthPrime(18)\mathrm{NthPrime}(18)Out6161

Known value · Wolfram MathematicalFunctionData, Prime

live ↗
Insin⁡(1NthPrime(2)π(1+Γ(NthPrime(2))))2+sin⁡(πNthPrime(2))2\sin(\frac{1}{\mathrm{NthPrime}(2)}π(1+Γ(\mathrm{NthPrime}(2))))^2+\sin(π\mathrm{NthPrime}(2))^2Out00

Known value · Wolfram MathematicalFunctionData, Prime, FunctionalEquations

live ↗
Ine(2iπΓ(NthPrime(2)))/NthPrime(2)−1e(−2iπ)/NthPrime(2)−1\frac{e^{(2i πΓ(\mathrm{NthPrime}(2)))/\mathrm{NthPrime}(2)}-1}{e^{(-2i π)/\mathrm{NthPrime}(2)}-1}Out11

Known value · Wolfram MathematicalFunctionData, Prime, FunctionalEquations

live ↗
Insin⁡(1NthPrime(3)π(1+Γ(NthPrime(3))))2+sin⁡(πNthPrime(3))2\sin(\frac{1}{\mathrm{NthPrime}(3)}π(1+Γ(\mathrm{NthPrime}(3))))^2+\sin(π\mathrm{NthPrime}(3))^2Out00

Known value · Wolfram MathematicalFunctionData, Prime, FunctionalEquations

live ↗
Ine(2iπΓ(NthPrime(3)))/NthPrime(3)−1e(−2iπ)/NthPrime(3)−1\frac{e^{(2i πΓ(\mathrm{NthPrime}(3)))/\mathrm{NthPrime}(3)}-1}{e^{(-2i π)/\mathrm{NthPrime}(3)}-1}Out11

Known value · Wolfram MathematicalFunctionData, Prime, FunctionalEquations

Possible issues #

n must be a positive integer; compute-engine leaves NthPrime(0) unevaluated

live ↗
InNthPrime(0)\mathrm{NthPrime}(0)OutNthPrime(0)\mathrm{NthPrime}(0)
Neat examples #

The 1000th prime

live ↗
InNthPrime(1 000)\mathrm{NthPrime}(1\,000)Out7 9197\,919
About
Examples42
Checked againstWolfram Language 42 agreeSageMath 36 agree2 errorSymPy 36 agree1 errorRust 30 agree2 differ1 error
CrosswalkWikipediaMathWorldWikidataEncyclopedia of MathematicsnLabBritannicaOEISWolfram LanguageSageMathSymPyRust

Implementation

mappedsympypackages/oracle/src/mappings.ts

prime(n) is SymPy's n-th prime.

mappedsagepackages/oracle/src/mappings.ts
mappedrustpackages/oracle/src/mappings.ts

See also: FactorInteger, PrimePi