Open In App

Different Methods to Check if a Number is Prime

Last Updated : 11 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Prime numbers are the natural numbers greater than 1, that have no positive divisors other than 1 and itself. In other words, a prime number can only be divided evenly (without a remainder) by 1 and the number itself.

We can also use the following calculator to check if any number is prime or composite.

Methods to Check if a Number is Prime or Not

Other then the calculator, we can also use the following manual methods to check if the given number is prime or composite.

  • Factorization
  • Trial Division Method
  • Fermat Primality Test
  • AKS Primality Test
  • Seive of Eratosthenes

Finding Prime Number by Factorization

Prime Factorization is the most common method to find whether a number is prime or not, by simply noting down the factors of the given number.

For example: Let's check whether 1221 is a prime number or not.

Prime-Factorization-of-1221

Factors of 1221 are 1, 3, 11, 33, 37, 111, 407, and 1221.

Since it has more than 2 factors, it is not a prime number.

Trial Division Method

Trial division method checks divisibility from 2 up to the square root of the number. If the number is divisible by any of these, it is not prime.

For example: Let's check whether 29 is a prime number or not using the trial division method.

As the square root of 29 is approximately 5.39, so we will check the divisibility by integers up to 5. Lets check divisibility of 29 by 2, 3, and 5.

  • Last digit of 29 is 9, which is odd, so 29 is not divisible by 2.
  • Sum of the digits of 29 is 2 + 9 = 11, and 11 is not divisible by 3. So, 29 is not divisible by 3.
  • Last digit of 29 is 9, which is neither 0 nor 5. So, 29 is not divisible by 5.

Since 29 is not divisible by any number up to 5, it is a prime number.

Fermat Primality Test

Fermat Primality Test is a probabilistic algorithm used to determine if a number is prime. It is based on Fermat's Little Theorem, which states that if p is a prime number, then for any integer a such that 1 ≤ a < p, the following congruence holds:

ap − 1 ≡ 1 (mod p)

Note: This test can give a false positive (declare a composite number as prime) for certain values of a, especially for Carmichael numbers.

AKS Primality Test

The AKS primality test was introduced by Manindra Agrawal, Neeraj Kayal, and Nitin Saxena in 2002. It is a significant algorithm used in number system to find whether a number is prime or not.

AKS primality test states: A number n is prime if and only if the following holds for all integers a:

(a + x)n ≡ an + xn (mod n)

Note: This test doesn't check for all values of a, instead using a selective set of values to make the algorithm efficient.

For Example: Check if n =7 is a prime or not.

  • Perfect Power Check:

A perfect power is a number that can be written as ab, where a >1 and b > 1. 7 is not the perfect power.

  • Set K :

Calculate k=⌊log⁡(7)⌋+1=1

  • Check the polynomial condition:

We need to verify if:

(x-1)7 ≡x7-1 (mod 7)

  • Expand (x−1)7 using the binomial theorem:

(x - 1)^7 = x^7 - 7x^6 + 21x^5 - 35x^4 + 35x^3 - 21x^2 + 7x - 1

  • Subtract the above equations:

(x - 1)^7 - (x^7 - 1) = -7x^6 + 21x^5 - 35x^4 + 35x^3 - 21x^2 + 7x

  • Check modulo n (7):

Substitute x=1:

-7(1)^6 + 21(1)^5 - 35(1)^4 + 35(1)^3 - 21(1)^2 + 7(1) = 0

Since the result is 0, n = 7 is confirmed to be prime.

Seive of Eratosthenes

Seive of Eratosthenes method finds all the prime number up to a certain limit starting from 2 as it is the smallest prime number.

For Example: To find the all the prime numbers up to 20:

  • Start with a list of numbers from 2 to 20.
  • Mark all the multiples of 2 (except 2) : 4, 6, 8, 10, 12, 14, 16, 18
  • Mark all the multiples of 3 (except 3) : 6, 9 , 12, 15, 18
  • Mark all the multiples of 5 (except 5) : 10, 15, 20

The remaining numbers are prime numbers: 2, 3, 5, 7, 11, 13, 17 ,19.

Conclusion

In summary, determining whether a number is prime can be approached through various methods, each with its advantages and limitations. Some methods are Factorization, Trial division, Fermat Primality Test, AKS Primality Test, Seive of Eratosthenes, etc. There are many more methods other then these to check any number whether it is prime or not.

Read More,


Next Article
Practice Tags :

Similar Reads