Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: ISC COMPUTER SCIENCE PRACTICAL 2002 SOLVED QUESTION 1 : TWIN PRIME

Saturday, March 16, 2019

ISC COMPUTER SCIENCE PRACTICAL 2002 SOLVED QUESTION 1 : TWIN PRIME


ISC COMPUTER SCIENCE PRACTICAL SOLVED

  Twin Primes are consecutive prime numbers whose difference is 2.

  For example, (3, 5), (11, 13), (17, 19) are all twin primes.
  We define the distance of any twin prime pair from n as 
  :- minimum (abs(n-p1), abs(n-p2))
  where (p1,p2) is a pair of twin prime.

  where abs( ) returns the absolute value of its argument and minimum( ) returns the        smaller of its two arguments.   

  Write a program that reads in a positive integer n and prints out the twin prime that    has least distance from n.

  For example if n is 30 the pair is (29, 31), if n is 13 pair is (11, 13), if n is 49      the pair is (41, 43).   
  If n is 54 the pair is (59, 61).

The logic is as follows:
1. Input the number N
2. Find the Twin prime just after or from N 
(it might happen the (N,N+2) is a twin prime)
3. Find the Twin prime just before N
4. Compared the distances of these Twin Primes from N using 
the formula given.
5. Print the Twin Prime having least distance from N.

The distance method is 
private int distanceOfTwinPrime(int N,int p) {

             return (Math.min(Math.abs(N-p),Math.abs(N-(p+2))));
            
       }


The method private int generateTwinPrime(int N,String s)
finds the "after" or "before" Twin Primes of N by sending
(N,"after") or (N,"before") as required:
The logic is if (N,N+2) is not a pair of twin prime we
increment N to N+1 and check for (N+1,N+1+2)
flag = false;

             while(flag==false) {
             if(isPrime(N)==true && isPrime(N+2)==true)   
                    flag=true;
             else
                    N++;
This particualr block finds the next Twin prime.
Similarly for (N,"before")
private boolean isPrime(int n) returns true in n is prime or else
it returns false.

private int distance_after;

private int distance_before;
stores the distance of N from the "after" Twin Prime
stores the distance of N from the "before" Twin Prime

For N = 1 and 2 The closest Twin prime is (3,5). This can be
printed straight forward.

JAVA CODE

import java.util.*;
public class ISC2002Q1 { 
    public static void main(String[] args) {
       int N;
       Scanner in = new Scanner(System.in);
       System.out.println("Enter the value of N:");
       N = in.nextInt();
       TwinPrime obj = new TwinPrime(N);
       obj.displayNearestTwinPrime();
       in.close();
        }  
    }
class TwinPrime {
       public void displayNearestTwinPrime() {
             flag = false;
             distance_after = distanceOfTwinPrime(number,generateTwinPrime(number,"after"));
             distance_before = distanceOfTwinPrime(number,generateTwinPrime(number,"before"));
             flag = distance_after < distance_before ? true : false;
             if(flag)
                    System.out.print("Nearest Twin Prime for "+number+" is"+"("+generateTwinPrime(number,"after")+","+(generateTwinPrime(number,"after")+2)+").");
             else
                    System.out.print("Nearest Twin Prime for "+number+" is"+"("+generateTwinPrime(number,"before")+","+(generateTwinPrime(number,"before")+2)+").");
            
       }
       private int distanceOfTwinPrime(int N,int p) {
             return (Math.min(Math.abs(N-p),Math.abs(N-(p+2))));
            
       }
       private int generateTwinPrime(int N,String s) { // if (p,p+2) is the twin prime it returns p
             if(s=="after") {
             flag = false;
             while(flag==false) {
             if(isPrime(N)==true && isPrime(N+2)==true)   
                    flag=true;
             else
                    N++;
             } // finds the pair of twin prime after N
            if(N<=2)
             return 3;
             else
                 return (N);
             }
             else {
                    flag = false;
                    while(flag==false) {
                    if(isPrime(N)==true && isPrime(N+2)==true)   
                          flag=true;
                    else
                          N--;
                    }
              if(N<=2)
              return 3;
             else
                 return (N);  // finds the pair of twin prime before N
             }
       }
       private boolean isPrime(int n) {
              flag = true;
              if (n==1)
                    return false;
         else {
                for(int i = 2; i<=n/2;i++)
                          if(n%i==0) {
                             flag=false;
                             break;
                          }
                return(flag);
              }
         }
       TwinPrime(int N){
             number = N;
       }
       private int number;
       private boolean flag;
       private int distance_after;
       private int distance_before;
}

2 comments:

  1. Dude why are you creating so much hassel just create a single class.👎

    ReplyDelete
    Replies
    1. Then why your are using JAVA use C! The essence of JAVA is object oriented programming. Concept or public and private, easily editable methods must be reflected in your program.

      Delete