Hello friends and welcome to GeeksforGeeks.
In this video, we’ll see how to check whether
the given number is prime or not.
Problem Statement says that given a positive
integer, check if the number is prime or not.
Basically, a prime number is a natural number
greater than 1 that has no positive divisors
other than 1 and itself. For eg, the numbers
shown here are prime because they have no
divisor other than 1 and itself. Our code
should return true if the number is prime
and false
if it’s not. Remember that 1 is neither
prime nor composite.
Solution 1. We now know a prime number is
a one which is only divisible by 1 and itself.
So,
what if we check n’s divisibility by all
numbers from 2 to n-1. If there’s no number
from 2 to
n-1 that divides n, it means only 1 and n
divide n and therefore, n is prime. Let’s
see the
code for this solution. We call the function
isPrime on the number. Now, obviously if the
number is smaller than or equal to 1, it’s
not prime. Now, we check the divisibility
of n by
numbers from 2 to n-1. If any number in this
range is a factor of n, we return false implying
the number is not prime. If there’s no factor
in the given range, we return true. It means
the
number is prime. The time complexity of this
solution is O(n).
Solution 2 uses a few more optimizations over
the previous technique. First of all, we don’t
need to check the divisibility for all numbers
till n-1. We can just check till root n because
a
large factor of n must be a multiplied by
a smaller factor that has already been checked.
For
eg, if n is 36, its sqrt is 6. Any factor
greater than 6 like 9 would be multiplied
with a smaller
factor i.e. 4 which we have already checked.
Similarly, next factor 12 is multiplied with
smaller factor 3 to get n but we have already
checked for 3. So, we just need to check till
sqrt of n. Also, it’s interesting to see
that any prime number except 2 and 3 is of
the form
6k+-1. This is so because, all integers can
be represented as 6k+i where i goes from 0
to 5.
But any number of form 6k+0, 6k+2 or 6k+4
would be divisible by 2, so it cannot be prime.
Also, any number of the form 6k+3 would be
divisible by 3. So, it cannot be prime either.
We
are only left with 6k+1 and 6k+5 which can
be effectively written as 6k+-1. But if a
number is
of the form 6k+-1, it doesn’t necessarily
mean that it is prime. We need to still check
if the
number is divisible by any factor of the form
6k+-1.
Let’s try to understand the code for this
solution. isPrime function is called on the
number
we need to check. Now, firstly we check if
number is less than or equal to 1, then it
cannot
be prime. If the number is 2 or 3, then it
is prime. Now, if the number n is divisible
by 2 or 3,
it is not prime. We now just need to check
if 6k+-1 is a factor of n or not. We start
the loop
from 5 which is of the form 6k-1. i goes till
square root of n and is incremented by 6 at
every
step. Now, we check if n is divisible by 6k-1
or 6k+1, then it is not prime. Since, i goes
over
6k-1, i+2 implies 6k+1. We return true at
the end implying the number is prime. So,
basically
if the number was greater than 1, not divisible
by 2 or 3 and did not have a factor of form
6k+-1, then it is prime. The time complexity
of this solution is O(root n).
Thank you for watching the video. Please leave
us your comments.
