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
Check Prime Number in Python
Given a positive integer N, the task is to write a Python program to check if the number is Prime or not in Python. For example, given a number 29, it has no divisors other than 1 and 29 itself. Hence, it is a prime number.Note: Negative numbers (e.g. -13) are not considered prime number.Using sympy
7 min read
Logic Gates - Definition, Types, Uses
Logic Gates are the fundamental building blocks in digital electronics. There are basically seven main types of logic gates that are used to perform various logical operations in digital systems. By combining different logic gates, complex operations are performed, and circuits like flip-flops, coun
10 min read
Bayes' Theorem
Bayes' Theorem is a mathematical formula that helps determine the conditional probability of an event based on prior knowledge and new evidence.It adjusts probabilities when new information comes in and helps make better decisions in uncertain situations.Bayes' Theorem helps us update probabilities
12 min read
Cloud Deployment Models
Cloud Computing has now become an essential part of modern businesses, offering flexibility, scalability, and cost-effective solutions. But Selecting the most appropriate cloud deployment model is essential to utilize the complete potential of cloud services. Whether you're a small business or a lar
12 min read
What is an IP Address?
Imagine every device on the internet as a house. For you to send a letter to a friend living in one of these houses, you need their home address. In the digital world, this home address is what we call an IP (Internet Protocol) Address. It's a unique string of numbers separated by periods (IPv4) or
14 min read
Numbers - Aptitude Questions and Answers
Numbers are the fundamental units of mathematics. In this article, we will discuss numbers, their types, important facts, divisibility rules, and other important points about numbers for aptitude preparation.Types of NumbersIntegers: All numbers whose fractional part is 0 (zero) like -3, -2, 1, 0, 1
14 min read
Software and its Types
Software is a collection of instructions, data, or computer programs that are used to run machines and carry out particular activities. It is the antithesis of hardware, which refers to a computer's external components. A device's running programs, scripts, and applications are collectively referred
7 min read
Inequalities in LaTeX
LaTeX is a powerful typesetting system widely used for creating high-quality scientific and technical documents. LaTeX offers precise and flexible tools for displaying a variety of inequalities, from simple linear ones to more intricate systems. In mathematics, an inequality represents a comparison
2 min read
Generations of Computers - Computer Fundamentals
The modern computer took its shape with the arrival of your time. It was around the 16th century when the evolution of the computer started. The initial computer faced many changes, obviously for the better. It continuously improved itself in terms of speed, accuracy, size, and price to urge the for
8 min read
Mean, Median and Mode
Mean, Median, and Mode are measures of the central tendency. These values are used to define the various parameters of the given data set. The measure of central tendency (Mean, Median, and Mode) gives useful insights about the data studied, these are used to study any type of data such as the avera
15+ min read