Open In App

Twin Prime Numbers

Last Updated : 07 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

A Twin prime are those numbers which are prime and having a difference of two ( 2 ) between the two prime numbers. In other words, a twin prime is a prime that has a prime gap of two. 
Sometimes the term twin prime is used for a pair of twin primes; an alternative name for this is prime twin or prime pair. Usually the pair (2, 3) is not considered to be a pair of twin primes. Since 2 is the only even prime, this pair is the only pair of prime numbers that differ by one; thus twin primes are as closely spaced as possible for any other two primes.
The first few twin prime pairs are : 
 

(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), 
(41, 43), (59, 61), (71, 73), (101, 103),
(107, 109), (137, 139), …etc.

FACT : There are 409 Twin primes below 10, 000.
Every twin prime pair except (3, 5) is of the form (6n – 1, 6n + 1) for some natural number n; that is, the number between the two primes is a multiple of 6.
Examples : 
 

Input : n1 = 11, n2 = 13
Output : Twin Prime

Input : n1 = 23, n2 = 37
Output : Not Twin Prime

 

Prerequisite : Primality Test | Set 1 (Introduction and School Method)
 

C++




// CPP program to check twin prime
#include <iostream>
using namespace std;
 
// Please refer below post for details of this
// function
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)  return false;
    if (n <= 3)  return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n%2 == 0 || n%3 == 0) return false;
  
    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;
  
    return true;
}
 
// Returns true if n1 and n2 are twin primes
bool twinPrime(int n1, int n2)
{
    return (isPrime(n1) && isPrime(n2) &&
                        abs(n1 - n2) == 2);
}
 
// Driver code
int main()
{
    int n1 = 11, n2 = 13;
    if (twinPrime(n1, n2))
        cout << "Twin Prime" << endl;
    else
        cout << endl
            << "Not Twin Prime" << endl;
    return 0;
}


Java




// JAVA Code for Twin Prime Numbers
import java.util.*;
 
class GFG {
 
    // Please refer below post for
    // details of this function
    static boolean isPrime(int n)
    {
        // Corner cases
        if (n <= 1) return false;
        if (n <= 3) return true;
 
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0)
            return false;
 
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
 
        return true;
    }
 
    // Returns true if n1 and n2 are twin primes
    static boolean twinPrime(int n1, int n2)
    {
        return (isPrime(n1) && isPrime(n2) &&
                     Math.abs(n1 - n2) == 2);
    }
 
    /* Driver program to test above function */
    public static void main(String[] args)
    {
        int n1 = 11, n2 = 13;
 
        if (twinPrime(n1, n2))
            System.out.println("Twin Prime");
        else
            System.out.println("Not Twin Prime");
    }
}
 
// This code is contributed by Arnav Kr. Mandal.


Python3




# Python3 code to check twin prime
import math
 
# Function to check whether a 
# number is prime or not
def isPrime( n ):
     
    # Corner cases
    if n <= 1:
        return False
    if n <= 3:
        return True
     
    # This is checked so that we
    # can skip middle five numbers
    # in below loop
    if n%2 == 0 or n%3 == 0:
        return False
     
    for i in range(5, int(math.sqrt(n)+1), 6):
        if n%i == 0 or n%(i + 2) == 0:
            return False
     
    return True
 
# Returns true if n1 and n2 are
# twin primes
def twinPrime(n1 , n2):
    return (isPrime(n1) and isPrime(n2) and
                    abs(n1 - n2) == 2)
 
# Driver code
n1 = 137
n2 = 139
 
if twinPrime(n1, n2):
    print("Twin Prime")
else:
    print("Not Twin Prime")
     
# This code is contributed by "Sharad_Bhardwaj".


C#




// C# Code for Twin Prime Numbers
using System;
 
class GFG {
 
    // Please refer below post for
    // details of this function
    static bool isPrime(int n)
    {
        // Corner cases
        if (n <= 1) return false;
        if (n <= 3) return true;
 
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0)
            return false;
 
        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
 
        return true;
    }
 
    // Returns true if n1 and n2 are twin primes
    static bool twinPrime(int n1, int n2)
    {
        return (isPrime(n1) && isPrime(n2) &&
                    Math.Abs(n1 - n2) == 2);
    }
 
    // Driver program
    public static void Main()
    {
        int n1 = 11, n2 = 13;
 
        if (twinPrime(n1, n2))
            Console.WriteLine("Twin Prime");
        else
            Console.WriteLine("Not Twin Prime");
    }
}
 
// This code is contributed by vt_m.


Javascript




<script>
// javascript Code for Twin Prime Numbers
 
    // Please refer below post for
    // details of this function
    function isPrime( n)
    {
     
        // Corner cases
        if (n <= 1)
            return false;
        if (n <= 3)
            return true;
 
        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0)
            return false;
 
        for ( let i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
 
        return true;
    }
 
    // Returns true if n1 and n2 are twin primes
    function twinPrime( n1, n2) {
        return (isPrime(n1) && isPrime(n2) && Math.abs(n1 - n2) == 2);
    }
 
    /* Driver program to test above function */
      
    let n1 = 11, n2 = 13;
 
    if (twinPrime(n1, n2))
        document.write("Twin Prime");
    else
        document.write("Not Twin Prime");
 
// This code is contributed by gauravrajput1
</script>


PHP




<?php
// PhP program to check twin prime
 
// Please refer below post for details
// of this function
 
function isPrime($n)
{
     
    // Corner cases
    if ($n <= 1) return false;
    if ($n <= 3) return true;
 
    // This is checked so that we can skip
    // middle five numbers in below loop
    if ($n % 2 == 0 || $n % 3 == 0)
        return false;
 
    for ($i = 5; $i * $i <= $n; $i = $i + 6)
        if ($n % $i == 0 || $n % ($i + 2) == 0)
            return false;
 
    return true;
}
 
// Returns true if n1 and n2 are twin primes
function twinPrime($n1, $n2)
{
    return (isPrime($n1) && isPrime($n2) &&
                        abs($n1 - $n2) == 2);
}
 
// Driver code
$n1 = 11; $n2 = 13;
if (twinPrime($n1, $n2))
    echo "Twin Prime", "\n";
else
    echo "\n", "Not Twin Prime", "\n";
 
// This code is contributed by ajit
?>


Output : 
 

Twin Prime

Time Complexity: O(?n1+?n2)
Auxiliary Space: O(1)
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads