WOLFRAM

PrimeQ[n]

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.

Examples

open allclose all

Basic Examples  (2)Summary of the most common use cases

Test whether a number is prime:

Out[1]=1

The number 4 is not prime:

Out[1]=1

Scope  (4)Survey of the scope of standard use cases

PrimeQ works over integers:

Out[1]=1

Gaussian integers:

Out[1]=1
Out[2]=2

Test for large integers:

Out[1]=1

PrimeQ threads over lists:

Out[1]=1

Options  (1)Common values & functionality for each option

GaussianIntegers  (1)

Test whether 5 is prime over integers:

Out[1]=1

Gaussian integers:

Out[2]=2

Applications  (22)Sample problems that can be solved with this function

Basic Applications  (5)

Highlight prime numbers:

Out[1]=1

Generate prime number:

Out[1]=1

Generate random prime numbers:

Out[1]=1
Out[2]=2

The distribution of Gaussian primes:

Out[1]=1

Find the first few prime powers that are not prime:

Out[1]=1

Special Sequences  (11)

Plot Gaussian primes:

Out[3]=3

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:

Out[4]=4
Out[5]=5

Plot Eisenstein primes:

Out[7]=7

The quadratic polynomial is prime for :

Out[1]=1

Recognize Fermat primes, prime numbers of the form :

Out[1]=1

The number is not a Fermat prime:

Out[2]=2

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:

Out[2]=2
Out[3]=3

Recognize Wieferich primes, prime numbers p such that divides :

Out[2]=2

There are only two known Wieferich primes:

Out[3]=3

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

Out[2]=2

Let be all numbers of the form :

Check that the product of two numbers is still in :

Out[2]=2

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

Find the first 10 Hilbert primes:

Out[4]=4

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

Out[1]=1

Find twin primes:

Out[1]=1

Find Mersenne prime exponents:

Out[1]=1
Out[2]=2

Number Theory  (6)

Find numbers that are prime over Gaussian integers and integers:

Out[2]=2

They are congruent to 3 mod 4:

Out[5]=5

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

Out[6]=6

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

Out[1]=1

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

Out[2]=2

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

Out[3]=3

Plot the difference between two consecutive primes:

Out[1]=1

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

Out[1]=1

The distribution of primes over integers:

Plot the distribution:

Out[3]=3

The distribution of Gaussian primes over Gaussian integers:

Plot the distribution:

Out[3]=3

Properties & Relations  (22)Properties of the function, and connections to other functions

Primes represents the domain of all prime numbers:

Out[1]=1

Prime gives prime number:

Out[1]=1
Out[2]=2

RandomPrime generates random prime numbers:

Out[1]=1
Out[2]=2

PrimePowerQ gives True for all prime numbers:

Out[1]=1
Out[2]=2

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

Out[1]=1
Out[2]=2
Out[3]=3

Prime powers are divisible by exactly one prime number:

Out[1]=1
Out[5]=5

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

Out[1]=1
Out[2]=2

The only even prime number is 2:

Out[1]=1

PrimeQ gives False for all composite numbers:

Out[4]=4
Out[5]=5

CompositeQ gives False for all primes:

Out[1]=1
Out[2]=2

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

Out[1]=1
Out[2]=2
Out[3]=3
Out[4]=4

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

Out[1]=1
Out[2]=2
Out[3]=3

The LCM for prime numbers is their product:

Out[1]=1
Out[2]=2

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

Out[1]=1
Out[2]=2

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

Out[1]=1
Out[2]=2

MersennePrimeExponents are prime numbers:

Out[1]=1

Use FactorInteger to find all prime divisors of a number:

Out[1]=1
Out[2]=2

PrimeOmega returns 1 for prime numbers:

Out[1]=1
Out[2]=2

PrimePi gives the number of primes:

Out[1]=1

The number of prime numbers up to 1000:

Out[2]=2

PrimeNu counts the number of prime divisors of a number:

Out[1]=1
Out[2]=2

Simplify expressions containing prime numbers:

Out[1]=1

Solve over Primes:

Out[1]=1

Interactive Examples  (1)Examples with interactive outputs

The polar plot of primes:

Out[1]=1

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:

Out[1]=1

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

Out[1]=1

Plot the Ulam spiral of prime numbers:

Out[2]=2
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).

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 ]}

@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 ]}

@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 ]}