Twin Prime Number in Java

Previous we developed the prime number program in java to check the given number is a prime number or not. Now, based on that program we will develop a twin prime number program in Java to check the given two numbers are prime twin numbers or not.

A pair of prime numbers having a difference of 2 is called a twin prime number. Sometimes it is also called a prime twin or prime pair. Example of prime twin are:- (3,5), (5,7), (11,13) e.t.c.

The even numbers are divisible by 2, so they can not be a prime number. Due to this reason, the prime twin can’t have an even number. In the given number if any number is even number then it can not be a prime twin. Since 2 is the only prime number which is even, and (2,4) is not a twin prime.

From the prime number program in java, we know that every prime number can be represented as 6n-1 or 6n+1. The twin prime number has a difference of 2. So, every twin prime except(3,5) in the form of (6n-1, 6n+1) for some natural number; that is the number between the twin prime number is a multiple of 6 (6n).

Java program to check twin prime number

import java.util.Scanner;

public class TwinPrimeNumber {

  public static boolean isPrime(int number) {

     // negative numbers, 0 and 1 are 
     // not a prime number
     if( number <= 1 ) return false;

     // 2 and 3 are prime numbers
     if( number <= 3 ) return true;

     // numbers divisible by 2 and 3
     // are not prime number
     if(number%2==0 || number%3==0)
        return false;

     // logic for remaining numbers
     for(int i=5; i<=Math.sqrt(number); i=i+6){
        if(number%i == 0 || number%(i+2) == 0) 
            return false;
     }

     return true;
  }

  public static boolean isTwinPrime(int a, int b){

     // if difference is not 2 then
     // it is not twin prime
     if(Math.abs(b-a) != 2) return false;

     // check both are prime number or not
     if(isPrime(a) && isPrime(b)) return true;
     // else return false  
     return false;
   }

   public static void main(String[] args) {

     // declare variables
     int n1 = 0, n2 = 0;

     // create Scanner class object to read input
     Scanner scan = new Scanner(System.in);

     // read number
     System.out.print("Enter two numbers:: ");
     n1 = scan.nextInt();
     n2 = scan.nextInt();

     // check twin prime and display result
     if(isTwinPrime(n1, n2))
        System.out.println(n1+" and "+ n2 + 
                 " are twin prime");
     else 
        System.out.println(n1+" and "+ n2 + 
                 " are not twin prime");

     // close Scanner class object
     scan.close();
  }
}

The output for different test-cases:-

Enter two numbers:: 2 3
2 and 3 are not twin prime

Enter two numbers:: 3 5
3 and 5 are twin prime.

Enter two numbers:: 5 7
5 and 7 are twin prime

The logic for the isTwinPrime() method also can be written in one line as,

public static boolean isTwinPrime(int a, int b){
   return ( (Math.abs(b-a) == 2) &&
          isPrime(a) && isPrime(b) );
}

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *