yields True if n is a prime number, and yields False otherwise.

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.


open allclose all

Basic Examples  (2)

Test whether a number is prime:

The number 4 is not prime:

Scope  (4)

PrimeQ works over integers:

Gaussian integers:

Test for large integers:

PrimeQ threads over lists:

Options  (1)

GaussianIntegers  (1)

Test whether 5 is prime over integers:

Gaussian integers:

Applications  (22)

Basic Applications  (5)

Highlight prime numbers:

Generate prime number:

Generate random prime numbers:

The distribution of Gaussian primes:

Find the first few prime powers that are not prime:

Special Sequences  (11)

Plot Gaussian primes:

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

Check wether an Eisenstein integer is prime:

Plot Eisenstein primes:

The quadratic polynomial is prime for :

Recognize Fermat primes, prime numbers of the form :

The number is not a Fermat prime:

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

The number 1729 is a Carmichael number; 1310 is not:

Recognize Wieferich primes, prime numbers p such that divides :

There are only two known Wieferich primes:

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

Let be all numbers of the form :

Check that the product of two numbers is still in :

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

Find the first 10 Hilbert primes:

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

Find twin primes:

Find Mersenne prime exponents:

Number Theory  (6)

Find numbers that are prime over Gaussian integers and integers:

They are congruent to 3 mod 4:

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

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

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

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

Plot the difference between two consecutive primes:

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

The distribution of primes over integers:

Plot the distribution:

The distribution of Gaussian primes over Gaussian integers:

Plot the distribution:

Properties & Relations  (22)

Primes represents the domain of all prime numbers:

Prime gives prime number:

RandomPrime generates random prime numbers:

PrimePowerQ gives True for all prime numbers:

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

Prime powers are divisible by exactly one prime number:

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

The only even prime number is 2:

PrimeQ gives False for all composite numbers:

CompositeQ gives False for all primes:

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

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

The LCM for prime numbers is their product:

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

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

MersennePrimeExponents are prime numbers:

Use FactorInteger to find all prime divisors of a number:

PrimeOmega returns 1 for prime numbers:

PrimePi gives the number of primes:

The number of prime numbers up to 1000:

PrimeNu counts the number of prime divisors of a number:

Simplify expressions containing prime numbers:

Solve over Primes:

Interactive Examples  (1)

The polar plot of primes:

Neat Examples  (3)

Visualize when is divisible by primes. Each row of dots corresponds to the divisors of , which are labeled along the horizontal axis:

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

Plot the Ulam spiral of prime numbers:

Wolfram Research (1988), PrimeQ, Wolfram Language function, (updated 2003).


Wolfram Research (1988), PrimeQ, Wolfram Language function, (updated 2003).


Wolfram Language. 1988. "PrimeQ." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2003.


Wolfram Language. (1988). PrimeQ. Wolfram Language & System Documentation Center. Retrieved from


@misc{reference.wolfram_2024_primeq, author="Wolfram Research", title="{PrimeQ}", year="2003", howpublished="\url{}", note=[Accessed: 20-July-2024 ]}


@online{reference.wolfram_2024_primeq, organization={Wolfram Research}, title={PrimeQ}, year={2003}, url={}, note=[Accessed: 20-July-2024 ]}