Different Methods to Check if a Number is Prime
Last Updated :
11 Oct, 2024
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.
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.
A perfect power is a number that can be written as ab, where a >1 and b > 1. 7 is not the perfect power.
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
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,
Similar Reads
Analysis of Different Methods to find Prime Number in Python
If you participate in competitive programming, you might be familiar with the fact that questions related to Prime numbers are one of the choices of the problem setter. Here, we will discuss how to optimize your function which checks for the Prime number in the given set of ranges, and will also cal
7 min read
Check if a number is Full Prime
A full prime number is one in which the number itself is prime and all its digits are also prime. Given a number n, check if it is Full Prime or not.Examples : Input : 53 Output : Yes Explanation: Number 53 is prime and its digits are also prime. Input : 41 Output : No Explanation: Number 41 is prim
7 min read
Check if the number is a Prime power number
Given an integer N, the task is to check if the number is a Prime power number. If yes, then print the number along with its power which is equal to N. Else print -1. A prime power is a positive integer power of a single prime number. For example: 7 = 71, 9 = 32 and 32 = 25 are prime powers, while 6
8 min read
Check if difference of areas of two squares is prime
Given two squares with side lengths a and b (a > b). The task is to check if difference of their areas is prime or not. Here side length could be large ( 1 < b < a < 1012).Examples: Input : a = 6, b = 5 Output : Yes Input : a = 61690850361, b = 24777622630 Output : No Approach: Since the
7 min read
Check if a number is Quartan Prime or not
Given a positive integer N, check if it is Quartan prime or not. Print 'Yes' if it is a Quartan prime otherwise Print 'No'.Quartan Prime : A prime number of the form x4 + y4 where x > 0, y > 0, and x and y are integers is a Quartan Prime. Quartan Prime in the range 1 - 100 are: 2, 17, 97 Examp
6 min read
Check if a number is Euler Pseudoprime
Given an integer N and a base number A, the task is to check whether N is a Euler Pseudoprime to the given base A. An integer N is called Euler Pseudoprime to the base A, if A > 0 and N is an odd composite number.A and N are co-prime i.e. GCD(A, N) = 1.A(N - 1) / 2 % N is either 1 or N - 1. Examp
9 min read
What are the methods to find prime numbers?
Answer: To find prime numbers, you can use methods like the Sieve of Eratosthenes, trial division, prime factorization, or online tools for quick identification.Here's a more detailed explanation:Sieve of Eratosthenes:Start with a list of numbers up to a certain limit.Mark 2 as prime and eliminate i
2 min read
Check if a number is Primorial Prime or not
Given a positive number N, the task is to check if N is a primorial prime number or not. Print 'YES' if N is a primorial prime number otherwise print 'NO.Primorial Prime: In Mathematics, A Primorial prime is a prime number of the form pn# + 1 or pn# - 1 , where pn# is the primorial of pn i.e the pro
10 min read
Check if N is a Weak Prime number or not
Given a positive integer N, the task is to check if N is a weak Prime or not. In number theory, a weak prime is a prime number that is less than the arithmetic mean of nearest prime numbers i.e next and previous prime numbers.First few weak prime numbers are 3, 7, 13, 19, 23, 31, 43, 47, 61, â¦A weak
8 min read
Check whether N is a Dihedral Prime Number or not
Given an integer N, the task is to check if N is a Dihedral prime number or not. A Dihedral prime is a prime number that can be read as itself or as another prime number when read in a seven-segment display, regardless of different orientation and surface. Examples: Input: N = 108881 Output: Yes Inp
10 min read