Un primo gemelo son aquellos números que son primos y tienen una diferencia de dos (2) entre los dos números primos. En otras palabras, un primo gemelo es un primo que tiene un espacio primo de dos.
A veces, el término primo gemelo se usa para un par de primos gemelos; un nombre alternativo para esto es gemelo primo o par primo. Por lo general, el par (2, 3) no se considera un par de primos gemelos. Dado que 2 es el único primo par, este par es el único par de números primos que difieren en uno; por lo tanto, los primos gemelos están lo más juntos posible para cualquier otro par de primos.
Los primeros pares primos gemelos son:
(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73), (101, 103), (107, 109), (137, 139), …etc.
HECHO : Hay 409 primos gemelos por debajo de 10, 000.
Cada par primo gemelo excepto (3, 5) es de la forma (6n – 1, 6n + 1) para algún número natural n; es decir, el número entre los dos números primos es un múltiplo de 6.
Ejemplos:
Input : n1 = 11, n2 = 13 Output : Twin Prime Input : n1 = 23, n2 = 37 Output : Not Twin Prime
Prerrequisito: Prueba de Primalidad | Conjunto 1 (Introducción y Método Escolar)
C++
// CPP program to check twin prime #include <iostream> using namespace std; // Please refer below post for details of this // function // https://goo.gl/Wv3fGv 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 // 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
# 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 // 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 // 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 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 ?>
Javascript
<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>
Producción :
Twin Prime
Publicación traducida automáticamente
Artículo escrito por nikunj_agarwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA