Skip to content

IsPrime ​

Tests whether n is a prime number.

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaPrimality testPrime numberMathWorldPrimalityTestPrimeNumberWikidataQ49008Encyclopedia of MathematicsPrime_numbernLabprime numberBritannicatopic/prime-numberOEISA000040Wolfram LanguagePrimeQ!Rosetta CodePrimality by trial division
IsPrime(n)tests whether nn is prime.
SageMathis_prime(n)✓SymPyisprime(n)Oscaris_prime(ZZ(n))Juliais_prime(ZZ(n))Rustis_prime(n)
IsPrime(n, Over -> GaussianIntegers)tests whether nn is prime in Z[i]\mathbb{Z}[i]; a complex nn is always tested there
IsPrime(n, Over -> QuadraticIntegers(d))tests whether nn is prime in the ring of integers of Q(d)\mathbb{Q}(\sqrt d), where an irreducible need not be prime
SageMathis_prime(d)✓SymPyisprime(d)Oscaris_prime(ZZ(d))Juliais_prime(ZZ(d))Rustis_prime(d)
Typed signatures
TypePackageApplies
(number | quadratic_integer, any*) -> booleannumber-theoryreplaces compute-enginealways
(number) -> booleancompute-enginealways

Domain: Number theory

Details
  • A prime has no positive divisors other than 1 and itself; 1 itself is not prime.
  • Returns False unless n is provably prime -- there's no third "unknown" outcome.
  • Threads element-wise over a list argument.
  • A negative n is not prime: compute-engine rules IsPrime(-7) False, a prime being a natural number. Wolfram's PrimeQ counts a prime's associates and answers True.

Examples

28 more kept as data, checked against the oracles
Basic #

1 is not prime by definition

live ↗
InIsPrime(1)\mathrm{IsPrime}(1)OutFalse\mathrm{False}

2 is the only even prime

live ↗
InIsPrime(2)\mathrm{IsPrime}(2)OutTrue\mathrm{True}
live ↗
InIsPrime(13)\mathrm{IsPrime}(13)OutTrue\mathrm{True}
live ↗
InIsPrime(4)\mathrm{IsPrime}(4)OutFalse\mathrm{False}
live ↗
InIsPrime(97)\mathrm{IsPrime}(97)OutTrue\mathrm{True}

Threads element-wise over a list

live ↗
InIsPrime([1,2,3,4,5,6])\mathrm{IsPrime}(\bigl\lbrack1, 2, 3, 4, 5, 6\bigr\rbrack)Out[False,True,True,False,True,False]\bigl\lbrack\mathrm{False}, \mathrm{True}, \mathrm{True}, \mathrm{False}, \mathrm{True}, \mathrm{False}\bigr\rbrack
live ↗
InIsPrime(5)\mathrm{IsPrime}(5)OutTrue\mathrm{True}
Scope #

-7 is not prime: a prime is a natural number, though Wolfram's PrimeQ counts its associate 7

live ↗
InIsPrime(−7)\mathrm{IsPrime}(-7)OutFalse\mathrm{False}

a Gaussian prime: its norm 5 is prime

live ↗
InIsPrime(2+i)\mathrm{IsPrime}(2+i)OutTrue\mathrm{True}

5=(2+i)(2−i)5 = (2 + i)(2 - i) splits in Z[i]\mathbb{Z}[i]

live ↗
InIsPrime(5,Rule(Over,GaussianIntegers))\mathrm{IsPrime}(5, \mathrm{Rule}(\mathrm{Over}, \mathrm{GaussianIntegers}))OutFalse\mathrm{False}

Large integers: 103000+110^{3000}+1 is divisible by 108+110^8+1

live ↗
InIsPrime(103 000+1)\mathrm{IsPrime}(10^{3\,000}+1)OutFalse\mathrm{False}
Applications #

A common choice of prime modulus for hashing, just above 10910^9

live ↗
InIsPrime(1 000 000 007)\mathrm{IsPrime}(1\,000\,000\,007)OutTrue\mathrm{True}

A Fermat prime, 28+12^8+1

live ↗
InIsPrime(257)\mathrm{IsPrime}(257)OutTrue\mathrm{True}

The Fermat number F7F_7 is composite

live ↗
InIsPrime(1+2128)\mathrm{IsPrime}(1+2^{128})OutFalse\mathrm{False}

Prime powers that are not primes

live ↗
InFilter(1..100,_1⁡↦IsPrimePower(_1⁡)∧¬IsPrime(_1⁡))\mathrm{Filter}(1..100, \operatorname{\_1}↦\mathrm{IsPrimePower}(\operatorname{\_1})∧¬\mathrm{IsPrime}(\operatorname{\_1}))Out[4,8,9,16,25,27,32,49,64,81]\bigl\lbrack4, 8, 9, 16, 25, 27, 32, 49, 64, 81\bigr\rbrack
Properties #

an odd prime stays prime in Z[i]\mathbb{Z}[i] exactly when p≡3(mod4)p \equiv 3 \pmod 4

live ↗
InIsPrime([2,3,5,7,11,13],Rule(Over,GaussianIntegers))\mathrm{IsPrime}(\bigl\lbrack2, 3, 5, 7, 11, 13\bigr\rbrack, \mathrm{Rule}(\mathrm{Over}, \mathrm{GaussianIntegers}))Out[False,True,False,True,True,False]\bigl\lbrack\mathrm{False}, \mathrm{True}, \mathrm{False}, \mathrm{True}, \mathrm{True}, \mathrm{False}\bigr\rbrack

37 has no divisor up to 37\sqrt{37}

live ↗
InIsPrime(37)\mathrm{IsPrime}(37)OutTrue\mathrm{True}

29, one of a twin pair with 31

live ↗
InIsPrime(29)\mathrm{IsPrime}(29)OutTrue\mathrm{True}

17, a Fermat prime 24+12^4+1

live ↗
InIsPrime(17)\mathrm{IsPrime}(17)OutTrue\mathrm{True}

Composite: 22⋅32^2\cdot 3

live ↗
InIsPrime(12)\mathrm{IsPrime}(12)OutFalse\mathrm{False}

23, the ninth prime

live ↗
InIsPrime(23)\mathrm{IsPrime}(23)OutTrue\mathrm{True}

Composite: 23⋅32^3\cdot 3

live ↗
InIsPrime(24)\mathrm{IsPrime}(24)OutFalse\mathrm{False}

Threads over a list

live ↗
InIsPrime([7,11])\mathrm{IsPrime}(\bigl\lbrack7, 11\bigr\rbrack)Out[True,True]\bigl\lbrack\mathrm{True}, \mathrm{True}\bigr\rbrack

Threads over a list; 2 is the even prime

live ↗
InIsPrime([5,7,2])\mathrm{IsPrime}(\bigl\lbrack5, 7, 2\bigr\rbrack)Out[True,True,True]\bigl\lbrack\mathrm{True}, \mathrm{True}, \mathrm{True}\bigr\rbrack

19, the eighth prime

live ↗
InIsPrime(19)\mathrm{IsPrime}(19)OutTrue\mathrm{True}

A prime is its own only prime divisor

live ↗
InDivisorSum(19,_1⁡↦_1⁡,IsPrime)\mathrm{DivisorSum}(19, \operatorname{\_1}↦\operatorname{\_1}, \mathrm{IsPrime})Out1919
live ↗
InFilter(Divisors(625),IsPrime)\mathrm{Filter}(\mathrm{Divisors}(625), \mathrm{IsPrime})Out[5]\bigl\lbrack5\bigr\rbrack
live ↗
InFilter(Divisors(2 434 500),IsPrime)\mathrm{Filter}(\mathrm{Divisors}(2\,434\,500), \mathrm{IsPrime})Out[2,3,5,541]\bigl\lbrack2, 3, 5, 541\bigr\rbrack
live ↗
InFilter(Divisors(65),IsPrime)\mathrm{Filter}(\mathrm{Divisors}(65), \mathrm{IsPrime})Out[5,13]\bigl\lbrack5, 13\bigr\rbrack
Possible issues #

Non-integers are not prime

live ↗
InIsPrime(12)\mathrm{IsPrime}(\frac{1}{2})OutFalse\mathrm{False}

A symbol stays unevaluated

live ↗
InIsPrime(x)\mathrm{IsPrime}(x)OutIsPrime(x)\mathrm{IsPrime}(x)
Neat examples #

The Mersenne prime 231−12^{31}-1

live ↗
InIsPrime(231−1)\mathrm{IsPrime}(2^{31}-1)OutTrue\mathrm{True}

The Fermat number F5=232+1F_5=2^{32}+1 is composite: 641 divides it

live ↗
InIsPrime(232+1)\mathrm{IsPrime}(2^{32}+1)OutFalse\mathrm{False}
About
Examples61
Checked againstWolfram Language 58 agree3 differSageMath 42 agree1 errorSymPy 26 agree17 errormathlib4 23 agree12 errorOscar 15 agree5 errorJulia 30 agree12 errorRust 24 agree8 error
CrosswalkWikipedia ×2MathWorld ×2WikidataEncyclopedia of MathematicsnLabBritannicaOEISWolfram LanguageSageMathSymPyOscarJuliaRustRosetta Code

Implementation

mappedwolframpackages/oracle/src/mappings.ts
mappedsympypackages/oracle/src/mappings.ts
mappedsagepackages/oracle/src/mappings.ts
mappedoscarpackages/oracle/src/mappings.ts
mappedjuliapackages/oracle/src/mappings.ts
mappedmathlib4packages/oracle/src/mappings.ts
mappedrustpackages/oracle/src/mappings.ts

See also: FactorInteger, NextPrime