PrimeQ
✖
PrimeQ
Details and Options

- PrimeQ is typically used to test whether an integer is a prime number.
- A prime number is a positive integer that has no divisors other than 1 and itself.
- PrimeQ[n] returns False unless n is manifestly a prime number.
- For negative integer n, PrimeQ[n] is effectively equivalent to PrimeQ[-n].
- With the setting GaussianIntegers->True, PrimeQ determines whether a number is a Gaussian prime.
- PrimeQ[m+In] automatically works over Gaussian integers.

Examples
open allclose allBasic Examples (2)Summary of the most common use cases
Scope (4)Survey of the scope of standard use cases
PrimeQ works over integers:

https://wolfram.com/xid/0wgi29f-u0o0br


https://wolfram.com/xid/0wgi29f-wo26j4


https://wolfram.com/xid/0wgi29f-nfavki


https://wolfram.com/xid/0wgi29f-845fn4

PrimeQ threads over lists:

https://wolfram.com/xid/0wgi29f-eulagn

Options (1)Common values & functionality for each option
Applications (22)Sample problems that can be solved with this function
Basic Applications (5)

https://wolfram.com/xid/0wgi29f-mi7qim


https://wolfram.com/xid/0wgi29f-m7tjfe

Generate random prime numbers:

https://wolfram.com/xid/0wgi29f-bk64lk


https://wolfram.com/xid/0wgi29f-c1yx4g

The distribution of Gaussian primes:

https://wolfram.com/xid/0wgi29f-b620o8

Find the first few prime powers that are not prime:

https://wolfram.com/xid/0wgi29f-bmhtty

Special Sequences (11)

https://wolfram.com/xid/0wgi29f-ej8xp5

https://wolfram.com/xid/0wgi29f-6y3qa

Eisenstein integers are complex numbers of the form where a and b are integers and ω is the cube root of unity
:

https://wolfram.com/xid/0wgi29f-mhbezn

https://wolfram.com/xid/0wgi29f-yfolsm
Check wether an Eisenstein integer is prime:

https://wolfram.com/xid/0wgi29f-iy83y0

https://wolfram.com/xid/0wgi29f-9cenw5


https://wolfram.com/xid/0wgi29f-khllhf


https://wolfram.com/xid/0wgi29f-dxrxgc

https://wolfram.com/xid/0wgi29f-qz4c9y

The quadratic polynomial is prime for
:

https://wolfram.com/xid/0wgi29f-dj4898

Recognize Fermat primes, prime numbers of the form :

https://wolfram.com/xid/0wgi29f-mj1gl7

The number is not a Fermat prime:

https://wolfram.com/xid/0wgi29f-jp8z0t

Recognize Carmichael numbers, composite numbers n that satisfy mod
for all integers b that are relatively prime to n:

https://wolfram.com/xid/0wgi29f-mjbk4f
The number 1729 is a Carmichael number; 1310 is not:

https://wolfram.com/xid/0wgi29f-8e5nta


https://wolfram.com/xid/0wgi29f-pwumyg

Recognize Wieferich primes, prime numbers p such that divides
:

https://wolfram.com/xid/0wgi29f-hndplo

https://wolfram.com/xid/0wgi29f-len25z

There are only two known Wieferich primes:

https://wolfram.com/xid/0wgi29f-3y6osb

Recognize Gaussian Mersenne primes, prime numbers n such that is a Gaussian prime:

https://wolfram.com/xid/0wgi29f-czd2jj

https://wolfram.com/xid/0wgi29f-b7rsdv

Let be all numbers of the form
:

https://wolfram.com/xid/0wgi29f-y7zsqg
Check that the product of two numbers is still in :

https://wolfram.com/xid/0wgi29f-61uqh7

Recognize Hilbert primes, prime numbers that have no divisors in other than 1 and itself:

https://wolfram.com/xid/0wgi29f-4pv3e7
Find the first 10 Hilbert primes:

https://wolfram.com/xid/0wgi29f-2swwye

Test whether or not the first 47 Mersenne prime exponents are prime:

https://wolfram.com/xid/0wgi29f-romquq


https://wolfram.com/xid/0wgi29f-ql3hbb

Find Mersenne prime exponents:

https://wolfram.com/xid/0wgi29f-n63f3p


https://wolfram.com/xid/0wgi29f-d69xgz

Number Theory (6)
Find numbers that are prime over Gaussian integers and integers:

https://wolfram.com/xid/0wgi29f-ttmtf

They are congruent to 3 mod 4:

https://wolfram.com/xid/0wgi29f-j1ne11

These numbers cannot be written as the sum of two squares:

https://wolfram.com/xid/0wgi29f-dn44j6

Find numbers that are composite over Gaussian integers but prime over integers:

https://wolfram.com/xid/0wgi29f-9f178g

All of them except for 2 are congruent to 1 mod 4:

https://wolfram.com/xid/0wgi29f-nllsgi

These numbers can be written as the sum of two squares in 8 ways:

https://wolfram.com/xid/0wgi29f-rd3ngt

Plot the difference between two consecutive primes:

https://wolfram.com/xid/0wgi29f-jmvbwl

The infinite sum of reciprocals of prime powers that are not prime converges:

https://wolfram.com/xid/0wgi29f-lu7z7a

The distribution of primes over integers:

https://wolfram.com/xid/0wgi29f-hi0e4n

https://wolfram.com/xid/0wgi29f-iul1cx

https://wolfram.com/xid/0wgi29f-d4lpp

The distribution of Gaussian primes over Gaussian integers:

https://wolfram.com/xid/0wgi29f-9om2p7

https://wolfram.com/xid/0wgi29f-1ullvw

https://wolfram.com/xid/0wgi29f-jrlfrw

Properties & Relations (22)Properties of the function, and connections to other functions
Primes represents the domain of all prime numbers:

https://wolfram.com/xid/0wgi29f-2wjcms

Prime gives prime number:

https://wolfram.com/xid/0wgi29f-bwxgxm


https://wolfram.com/xid/0wgi29f-m3768e

RandomPrime generates random prime numbers:

https://wolfram.com/xid/0wgi29f-b2r76k


https://wolfram.com/xid/0wgi29f-l1e1p

PrimePowerQ gives True for all prime numbers:

https://wolfram.com/xid/0wgi29f-g0tfh2


https://wolfram.com/xid/0wgi29f-b2ds79

Primes that are congruent to 1 mod 4 are not prime powers in the Gaussian integers:

https://wolfram.com/xid/0wgi29f-i0nw8j


https://wolfram.com/xid/0wgi29f-7k9zwv


https://wolfram.com/xid/0wgi29f-0ahika

Prime powers are divisible by exactly one prime number:

https://wolfram.com/xid/0wgi29f-j0ie3


https://wolfram.com/xid/0wgi29f-dfj2iy

The only divisors of a prime number p is 1 and p:

https://wolfram.com/xid/0wgi29f-bj49w7


https://wolfram.com/xid/0wgi29f-ioq1b5

The only even prime number is 2:

https://wolfram.com/xid/0wgi29f-dz613q

PrimeQ gives False for all composite numbers:

https://wolfram.com/xid/0wgi29f-cocnkk


https://wolfram.com/xid/0wgi29f-dz14y

CompositeQ gives False for all primes:

https://wolfram.com/xid/0wgi29f-4sc00


https://wolfram.com/xid/0wgi29f-jf1u0m

Every integer greater than 1 either is a prime number itself or can be represented as the product of prime numbers:

https://wolfram.com/xid/0wgi29f-ky3hs2


https://wolfram.com/xid/0wgi29f-e7lr3s


https://wolfram.com/xid/0wgi29f-fjuubb


https://wolfram.com/xid/0wgi29f-d2b1t6

The GCD of two prime numbers is 1; consequently, two prime numbers are relatively prime:

https://wolfram.com/xid/0wgi29f-fd85es


https://wolfram.com/xid/0wgi29f-cq1ayy


https://wolfram.com/xid/0wgi29f-cwgicu

The LCM for prime numbers is their product:

https://wolfram.com/xid/0wgi29f-c11ruk


https://wolfram.com/xid/0wgi29f-b6ga13

The sum of the prime divisors of a prime number returns the original number:

https://wolfram.com/xid/0wgi29f-km73bd


https://wolfram.com/xid/0wgi29f-7qsbw7

Prime numbers of the form , where the exponent p is also prime, are called Mersenne primes:

https://wolfram.com/xid/0wgi29f-djva1f


https://wolfram.com/xid/0wgi29f-dxitd8

MersennePrimeExponents are prime numbers:

https://wolfram.com/xid/0wgi29f-i7supp

Use FactorInteger to find all prime divisors of a number:

https://wolfram.com/xid/0wgi29f-pefmwt


https://wolfram.com/xid/0wgi29f-ona38t

PrimeOmega returns 1 for prime numbers:

https://wolfram.com/xid/0wgi29f-ok5wcm


https://wolfram.com/xid/0wgi29f-do9kut

PrimePi gives the number of primes:

https://wolfram.com/xid/0wgi29f-7ikr8v

The number of prime numbers up to 1000:

https://wolfram.com/xid/0wgi29f-co0tmt

PrimeNu counts the number of prime divisors of a number:

https://wolfram.com/xid/0wgi29f-u4r83x


https://wolfram.com/xid/0wgi29f-c3zirp

Simplify expressions containing prime numbers:

https://wolfram.com/xid/0wgi29f-7w1yvx

Solve over Primes:

https://wolfram.com/xid/0wgi29f-vjjf3s

Interactive Examples (1)Examples with interactive outputs
Neat Examples (3)Surprising or curious use cases
Visualize when is divisible by primes. Each row of dots corresponds to the divisors of
, which are labeled along the horizontal axis:

https://wolfram.com/xid/0wgi29f-eq61yv

Plot the prime numbers that are the sum of three squares:

https://wolfram.com/xid/0wgi29f-l9t

Plot the Ulam spiral of prime numbers:

https://wolfram.com/xid/0wgi29f-l5q7y3

https://wolfram.com/xid/0wgi29f-vcxncv

Wolfram Research (1988), PrimeQ, Wolfram Language function, https://reference.wolfram.com/language/ref/PrimeQ.html (updated 2003).
Text
Wolfram Research (1988), PrimeQ, Wolfram Language function, https://reference.wolfram.com/language/ref/PrimeQ.html (updated 2003).
Wolfram Research (1988), PrimeQ, Wolfram Language function, https://reference.wolfram.com/language/ref/PrimeQ.html (updated 2003).
CMS
Wolfram Language. 1988. "PrimeQ." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2003. https://reference.wolfram.com/language/ref/PrimeQ.html.
Wolfram Language. 1988. "PrimeQ." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2003. https://reference.wolfram.com/language/ref/PrimeQ.html.
APA
Wolfram Language. (1988). PrimeQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PrimeQ.html
Wolfram Language. (1988). PrimeQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/PrimeQ.html
BibTeX
@misc{reference.wolfram_2025_primeq, author="Wolfram Research", title="{PrimeQ}", year="2003", howpublished="\url{https://reference.wolfram.com/language/ref/PrimeQ.html}", note=[Accessed: 18-May-2025
]}
BibLaTeX
@online{reference.wolfram_2025_primeq, organization={Wolfram Research}, title={PrimeQ}, year={2003}, url={https://reference.wolfram.com/language/ref/PrimeQ.html}, note=[Accessed: 18-May-2025
]}