in this lesson we're going to write a simple
program
to verify
whether a given number N
is prime or not
so what is a prime number
a prime number is a positive integer
that can be divided by exactly 2
numbers
1 and the number itself
so let us quickly see what all
numbers can be called prime numbers
one and
if we draw a set of that can
divide one, we get only one
one is not prime number because for a number
to be prime it should be divided by exactly
two numbers
2 can be divided by 1
and 2
this is a prime number
3 again can be divided by one and 3 and
this is a prime number
4 can be divided by one
1, 2 and 4. This is not a prime number
and they can go on. so the smallest prime
no is 2
so the simplest thing that we can do
to verify whether a given no N is prime or not
is to try dividing it
by all thenumbers
greater than 1 and less than N, that is
for i starting 2 to N-1
if there exists
any such i
that divides them
then
N is not prime
if we can come out of this loop
without finding any such i, then N is prime
and let us just assume that
for the special case when N is less than 1 or equal to 1
we handle it correctly
in the actual program
now a simple observation here
is that
there can not be a factor of N other than N itself
that can be greater than N/2
so we cannot find a factor of
N greater than N/2
and less than or equal to N minus one
so we do not need to run the loop all the way till N minus one and only check till
n /2 
should be sufficient
this sounds good
but can we do something better here
let us see
if a
divides n
then there must exist
another number b
that should be equal to n/a
and b should also divide n
so we should have a relationship like
a X b = N
a and b
are called co-factors
so all the factors of n always exist is pairs
and it'll be
for the theory
I use an example.
let's pick up a number
thirty six
and try to see that what all
pair of factors are attached
first factor of thirty six is one
if you say that a is equal to one
then b must be equal to thirty six by one
which is
thirty six
next pair of co-factors would be 2
and thirty six by two
12
sorry it should be
18
3 and 12, 4 and 9
six
and thirty six by six is  six
this is a special case where a is equal
to b
next factor is nine and the corresponding 
co-factor is 4
twelve and 3
eighteen and two
and finally thirty six and one
if you carefully see we can see a pattern here
that the number pairs are repeated
only the order is 
different but the numbers are the same
so we have 9,4 and 4,9
twelve,three three,twelve
eighteen,two two,eighteen.
 thirty six,one one,thirty six
so we are getting the same pairs again
for and b there can be
a case
where a
is equal to b
and that would happen only if n is a
perfect square and in this case
the equation will
translate to a square equal to n
or a is equal to
square root of n
in ourexample twenty six is a perfect
square. So we are able to get
such a pair of co-factors
where a is equal to b
what if a is not equal to b
so if a
for example is less than b
then see carefully a
should always be less than square root of n
and b
should be greater than square root of n
similarly if b
is less than a
then b should be
less than squared root of n
and a should be greater than square root of n
so if a and b are not equal
one of them
has to be less than square root of n and another one has to be greater than square root of n
let's try to plot alll these co-factors on a number-one line and they
should be
further clear to you.
so i qm drawing a number line here and it
is not so accurate
we can only try to approximate the position of the numbers
on this line
let's pick up the first
pair of factors
one should be somewhere here
and that is here
two should be some where here
and eighteen some where mid way between
zero and thirty six
we should be here and twelve some where here
four should be here and nine should be some
where here
so we can see that
one of the numbers in the pair
is less than square root of n 
and another
is greater than square root of n
and of course we should also plot six
and six where
a and b are both equal to square root of n
this four cases are just repetition of
our previous first four pairs
and we have already plotted them
we have shown it for n equals to 36 but this property is true for
all values of n
so now we can say that hey if i do not get
a factor of n all the way of square root of n
then I can clearly say
that i will not be able to get a factor
after square root of n as well
so all i need to do
is check only till square root of n
so in my actual program
I do not need to run the loop till n by 2
we can run the loop till square root of n and that should be enough
running loop till square root of n is lot more computationally
efficient and you could pick up some
values of n and then try to see that
square root of n
is lot lesser than n by 2
for higher values of n
so this is the pseudo code we leave
the rest of the implementation to you
take care of the special cases where n can less than and equal to one
the next lesson we will discus another
algorithm
for primality testing which is a very
famous algorithm
called Sieve of Eratosthenes
so thanks for watching
