Twin Prime Numbers

24 Mar, 2021
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++
Java
Python3
C#
PHP
Javascript
// CPP program to check twin prime#include <iostream>using namespace std; // Please refer below post for details of this// function// https://goo.gl/Wv3fGvbool 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 primesbool twinPrime(int n1, int n2){    return (isPrime(n1) && isPrime(n2) &&                        abs(n1 - n2) == 2);} // Driver codeint main(){    int n1 = 11, n2 = 13;    if (twinPrime(n1, n2))        cout << "Twin Prime" << endl;    else        cout << endl            << "Not Twin Prime" << endl;    return 0;}
// JAVA Code for Twin Prime Numbersimport java.util.*; class GFG {     // Please refer below post for    // details of this function    // https://goo.gl/Wv3fGv    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 code to check twin primeimport math # Function to check whether a # number is prime or notdef 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 primesdef twinPrime(n1 , n2):    return (isPrime(n1) and isPrime(n2) and                    abs(n1 - n2) == 2) # Driver coden1 = 137n2 = 139 if twinPrime(n1, n2):    print("Twin Prime")else:    print("Not Twin Prime")     # This code is contributed by "Sharad_Bhardwaj".
// C# Code for Twin Prime Numbersusing System; class GFG {     // Please refer below post for    // details of this function    // https://goo.gl/Wv3fGv    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.
<?php// PhP program to check twin prime // Please refer below post for details// of this function// https://goo.gl/Wv3fGv 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 primesfunction 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?>
<script>// javascript Code for Twin Prime Numbers     // Please refer below post for    // details of this function    // https://goo.gl/Wv3fGv    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>
Output :  
Twin Prime
 
jit_t
GauravRajput1
Prime Number
Mathematical
Mathematical
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Minimum number of jumps to reach end
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Find minimum number of coins that make a given value
Program for Decimal to Binary Conversion
Modulo 10^9+7 (1000000007)
Modulo Operator (%) in C/C++ with Examples
Program for factorial of a number