CREATE OWN LIBRARY

Twin Prime

Back to Programming

Description

Twin prime numbers are two prime numbers whose difference is 2, and both the numbers are prime numbers.

If both the numbers are prime but the difference is not 2, then the pair of the numbers are not twin prime.

The twin prime numbers are (3, 5), (5, 7), (11, 13) etc.

 But (2, 3), (7, 11) these pairs are not twin prime as the difference between two is not 2.

Algorithm

INPUT: Two Numbers

OUTPUT: Whether the numbers are twin prime or not

PROCESS:

Step 1: [Taking the inputs]

               Read m, n

Step 2: [Checking for ‘twin prime’]

               Set c<-0

               [Checking for twin prime]

               If absolute value of (m-n)=2 then

                              [Checking if m is a prime number]

                              For i=1 to m repeat          

                                             If m mod i=0 then

                                                            Set c<-c+1

                                             [End of ‘if’]

                              [End of ‘for’ loop]

                              [If the 1st number is prime then check for another number]

                              If c=2 then

                                             Set c<-0

                                             For i=1 to n repeat

                                                            If n mod i=0 then

                                                                           Set c<-c+1

                                                            [End of ‘if’]         

                                             [End of ‘for’ loop]            

                                             [Checking if n is a prime number]

                                             If c=2 then

                                                            Print "The numbers are twin prime"

                                             [If the 2nd number is not prime]

                                             Else

                                                            Print "The numbers are not twin prime"

                                             [End of ‘if-else’]

                              [End of ‘if’]

                              [If the 1st number is not prime]

                              Else

                                             Print "The numbers are not twin prime"

                              [End of ‘else’] 

               [End of ‘if’]

               [If the gap is not 2]

               Else

                              Print "The numbers are not twin prime" 

               [End of ‘Else’]

Step 3: Stop.

Code

TIME COMPLEXITY:

for(i=1;i<=m;i++)-------------------------------------O(m)            

                              {

                                             if(m%i==0)----------------------------------O(1)

                                                            c++;

                              }

                              //if the 1st number is prime then check for another number

                              if(c==2)--------------------------------------------------O(1)

                              {

                                             c=0;

                                             for(i=1;i<=n;i++)----------------------------O(n)

                                             {

                                                            if(n%i==0)--------------------------O(1)

                                                                           c++;       

                                             }              

                                             //checking if n is a prime number

                                             if(c==2)----------------------------------------O(1)

                                                            printf("The numbers are twin prime");

                                             //if the 2nd number is not prime

                                             else

                                                            printf("The numbers are not twin prime"); 

                              }

                              //if the 1st number is not prime

                              else

                                             printf("The numbers are not twin prime"); 

The time complexity of this program is O(m+n) where ‘m’ is the first number and ‘n’ is the second number.

SPACE COMPLEXITY:

The space complexity of this program is O(1) as it requires a constant number of memory spaces for any given input.