Prime Number Theorem: The probability that a given, randomly chosen number n is prime is inversely proportional to its number of digits, or to the logarithm of n.
Lemoine’s Conjecture: Any odd integer greater than 5 can be expressed as a sum of an odd prime (all primes other than 2 are odd) and an even semiprime. A semiprime number is a product of two prime numbers. This is called Lemoine’s conjecture.
Properties of prime numbers:
Every number greater than 1 can be divided by at least one prime number.
Every even positive integer greater than 2 can be expressed as the sum of two primes.
Except 2, all other prime numbers are odd. In other words, we can say that 2 is the only even prime number.
Two prime numbers are always coprime to each other.
Each composite number can be factored into prime factors and individually all of these are unique in nature.
Prime numbers and co-prime numbers:
It is important to distinguish between prime numbers and co-prime numbers. Listed below are the differences between prime and co-prime numbers.
A coprime number is always considered as a pair, whereas a prime number is considered as a single number.
Co-prime numbers are numbers that have no common factor except 1. In contrast, prime numbers do not have such a condition.
A co-prime number can be either prime or composite, but its greatest common factor (GCF) must always be 1. Unlike composite numbers, prime numbers have only two factors, 1 and the number itself.
Example of co-prime: 13and 15 are co-primes. The factors of 13 are 1 and 13 and the factors of 15 are 1, 3 and 5. We can see that they have only 1 as their common factor, therefore, they are coprime numbers.
Example of prime: A few examples of prime numbers are 2, 3, 5, 7 and 11 etc.
How do we check whether a number is Prime or not?
Naive Approach: A naive solution is to iterate through all numbers from 2 to sqrt(n) and for every number check if it divides n. If we find any number that divides, we return false.
Efficient approach: To check wheather the number is prime or not follow the below idea:
In the previous approach given if the size of the given number is too large then its square root will be also very large, so to deal with large size input we will deal with a few numbers such as 1, 2, 3, and the numbers which are divisible by 2 and 3 in separate cases and for remaining numbers, we will iterate our loop from 5 to sqrt(n) and check for each iteration whether that (iteration) or (that iteration + 2) divides n or not. If we find any number that divides, we return false.
Below is the implementation for the above idea:
C++
// A school method based C++ program to
// check if a number is prime
#include <bits/stdc++.h>
usingnamespacestd;
// function check whether a number
// is prime or not
boolisPrime(intn)
{
// Check if n=1 or n=0
if(n <= 1)
returnfalse;
// Check if n=2 or n=3
if(n == 2 || n == 3)
returntrue;
// Check whether n is divisible by 2 or 3
if(n % 2 == 0 || n % 3 == 0)
returnfalse;
// Check from 5 to square root of n
// Iterate i by (i+6)
for(inti = 5; i <= sqrt(n); i = i + 6)
if(n % i == 0 || n % (i + 2) == 0)
returnfalse;
returntrue;
}
// Driver Code
intmain()
{
isPrime(11) ? cout<<"true\n": cout<<"false\n";
return0;
}
// This code is contributed by Suruchi kumari
C
// A school method based C program to
// check if a number is prime
#include <stdio.h>
#include<math.h>
// function check whether a number
// is prime or not
intisPrime(intn)
{
// Check if n=1 or n=0
if(n <= 1)
return0;
// Check if n=2 or n=3
if(n == 2 || n == 3)
return1;
// Check whether n is divisible by 2 or 3
if(n % 2 == 0 || n % 3 == 0)
return0;
// Check from 5 to square root of n
// Iterate i by (i+6)
for(inti = 5; i <= sqrt(n); i = i + 6)
if(n % i == 0 || n % (i + 2) == 0)
return0;
return1;
}
// Driver Code
intmain()
{
if(isPrime(11) == 1)
printf("true\n");
else
printf("false\n");
return0;
}
// This code is contributed by Suruchi Kumari
Java
// Java program to check whether a number
classGFG
{
// Function check whether a number
// is prime or not
publicstaticbooleanisPrime(intn)
{
if(n <= 1)
returnfalse;
// Check if n=2 or n=3
if(n == 2|| n == 3)
returntrue;
// Check whether n is divisible by 2 or 3
if(n % 2== 0|| n % 3== 0)
returnfalse;
// Check from 5 to square root of n
// Iterate i by (i+6)
for(inti = 5; i <= Math.sqrt(n); i = i + 6)
if(n % i == 0|| n % (i + 2) == 0)
returnfalse;
returntrue;
}
// Driver Code
publicstaticvoidmain(String[] args)
{
if(isPrime(11))
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}
// This code is contributed by Sayan Chatterjee
Python3
# function check whether a number
# is prime or not
frommath import*
defisPrime(n):
# Check if n=1 or n=0
if(n <=1):
return"false"
# Check if n=2 or n=3
if(n ==2orn ==3):
return"true"
# Check whether n is divisible by 2 or 3
if(n %2==0orn %3==0):
return"false"
# Check from 5 to square root of n
# Iterate i by (i+6)
fori inrange(5, floor(sqrt(n)),6):
if(n %i ==0orn %(i +2) ==0):
return"false"
return"true"
ifisPrime(11):
print("true\n")
else:
print("false\n");
# This code is contributed by uomkar369
Output :
true
Time complexity: O(sqrt(n)) Auxiliary space: O(1)
Approach 3: To check the number is prime or not using recursion follow the below idea:
Recursion can also be used to check if a number between 2to n – 1 divides n. If we find any number that divides, we return false.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy