Dado un rango [bajo…alto], imprime los números gemelos más grandes en el rango dado (bajo y alto inclusive). Dos números son gemelos si son primos y la diferencia es 2.
Ejemplos:
Input: low = 10, high = 100 Output: Largest twins in given range: (71, 73) Input: low = 1, high = 20 Output: Largest twins in given range: (17, 19)
Una solución simple es comenzar desde alto y para cada número x verificar si x y x – 2 son primos y no lo son. Aquí x varía de mayor a menor + 2.
Una solución eficiente es usar la criba de Eratóstenes :
- Cree una array booleana «prime[0..high]» e inicialice todas las entradas como verdaderas. Un valor en primo[i] finalmente será falso si i no es un número primo, de lo contrario será verdadero.
- Ejecute un ciclo de p = 2 a alto.
- Si primo[p] es verdadero, entonces p es primo.
- Marque todos los múltiplos de p como no primos en primo[].
- Ejecute un ciclo de mayor a menor e imprima los primeros gemelos usando prime[] integrado en el paso 2.
C++
// C++ program to find the largest twin in given range #include <bits/stdc++.h> using namespace std; // Function to find twins void printTwins(int low, int high) { // Create a boolean array "prime[0..high]" and initialize // all entries it as true. A value in prime[i] will finally // be false if i is Not a prime, else true. bool prime[high + 1], twin = false; memset(prime, true, sizeof(prime)); prime[0] = prime[1] = false; // Look for the smallest twin for (int p = 2; p <= floor(sqrt(high)) + 1; p++) { // If p is not marked, then it is a prime if (prime[p]) { // Update all multiples of p for (int i = p * 2; i <= high; i += p) prime[i] = false; } } // Now print the largest twin in range for (int i = high; i >= low; i--) { if (prime[i] && (i - 2 >= low && prime[i - 2] == true)) { cout << "Largest twins in given range: (" << i - 2 << ", " << i << ")"; twin = true; break; } } if (twin == false) cout << "No such pair exists" << endl; } // Driver program int main() { printTwins(10, 100); return 0; }
Java
// Java program to find the // largest twin in given range import java.io.*; class GFG { // Function to find twins static void printTwins(int low, int high) { // Create a boolean array // "prime[0..high]" and initialize // all entries it as true. A value // in prime[i] will finally be false // if i is Not a prime, else true. boolean prime[] = new boolean[high + 1]; boolean twin = false; for(int i = 0; i < high + 1; i++) prime[i] = true; prime[0] = prime[1] = false; // Look for the smallest twin for (int p = 2; p <= Math.floor(Math.sqrt(high)) + 1; p++) { // If p is not marked, // then it is a prime if (prime[p]) { // Update all multiples of p for (int i = p * 2; i <= high; i += p) prime[i] = false; } } // Now print the largest twin in range for (int i = high; i >= low; i--) { if (prime[i] && (i - 2 >= low && prime[i - 2] == true)) { System.out.println("Largest twins in given range: (" + (i - 2) + ", " + (i) + ")"); twin = true; break; } } if (twin == false) System.out.println("No such pair exists"); } // Driver Code public static void main (String[] args) { printTwins(10, 100); } } // This code is contributed // by inder_verma.
Python3
# Python 3 program to find the largest twin # in given range # Function to find twins from math import sqrt,floor def printTwins(low, high): # Create a boolean array "prime[0..high]" # and initialize all entries it as true. # A value in prime[i] will finally # be false if i is Not a prime, else true. prime = [True for i in range(high + 1)] twin = False prime[0] = False prime[1] = False # Look for the smallest twin k = floor(sqrt(high)) + 2 for p in range(2, k, 1): # If p is not marked, then it # is a prime if (prime[p]): # Update all multiples of p for i in range(p * 2, high + 1, p): prime[i] = False # Now print the largest twin in range i = high while(i >= low): if (prime[i] and (i - 2 >= low and prime[i - 2] == True)): print("Largest twins in given range:(", (i - 2), ",", (i), ")") twin = True break i -= 1 if (twin == False): print("No such pair exists") # Driver Code if __name__ == '__main__': printTwins(10, 100) # This code is contributed by # Sanjit_Prasad
C#
// C# program to find the // largest twin in given range class GFG { // Function to find twins static void printTwins(int low, int high) { // Create a boolean array // "prime[0..high]" and initialize // all entries it as true. A value // in prime[i] will finally be false // if i is Not a prime, else true. bool[] prime = new bool[high + 1]; bool twin = false; for(int i = 0; i < high + 1; i++) prime[i] = true; prime[0] = prime[1] = false; // Look for the smallest twin for (int p = 2; p <= System.Math.Floor( System.Math.Sqrt(high)) + 1; p++) { // If p is not marked, // then it is a prime if (prime[p]) { // Update all multiples of p for (int i = p * 2; i <= high; i += p) prime[i] = false; } } // Now print the largest twin in range for (int i = high; i >= low; i--) { if (prime[i] && (i - 2 >= low && prime[i - 2] == true)) { System.Console.WriteLine("Largest twins in given range: (" + (i - 2) + ", " + (i) + ")"); twin = true; break; } } if (twin == false) System.Console.WriteLine("No such pair exists"); } // Driver Code public static void Main() { printTwins(10, 100); } } // This code is contributed // by mits
PHP
<?php //PHP program to find the largest twin in given range // Function to find twins function printTwins($low, $high) { // Create a boolean array "prime[0..high]" and initialize // all entries it as true. A value in prime[i] will finally // be false if i is Not a prime, else true. $prime[$high + 1]=array(); $twin = false; $prime = array_fill(0, ($high + 1), true); $prime[0] = $prime[1] = false; // Look for the smallest twin for ($p = 2; $p <= floor(sqrt($high)) + 1; $p++) { // If p is not marked, then it is a prime if ($prime[$p]) { // Update all multiples of p for ($i = $p * 2; $i <= $high; $i += $p) $prime[$i] = false; } } // Now print the largest twin in range for ($i = $high; $i >= $low; $i--) { if ($prime[$i] && ($i - 2 >= $low && $prime[$i - 2] == true)) { echo "Largest twins in given range: (", $i - 2 , ", " , $i , ")"; $twin = true; break; } } if ($twin == false) echo "No such pair exists" ; } // Driver program printTwins(10, 100); #This code is contributed by ajit. ?>
Javascript
<script> // Javascript program to find the // largest twin in given range // Function to find twins function printTwins(low, high) { // Create a boolean array // "prime[0..high]" and initialize // all entries it as true. A value // in prime[i] will finally be false // if i is Not a prime, else true. let prime = new Array(high + 1); let twin = false; for(let i = 0; i < high + 1; i++) prime[i] = true; prime[0] = prime[1] = false; // Look for the smallest twin for (let p = 2; p <= Math.floor(Math.sqrt(high)) + 1; p++) { // If p is not marked, // then it is a prime if (prime[p]) { // Update all multiples of p for (let i = p * 2; i <= high; i += p) prime[i] = false; } } // Now print the largest twin in range for (let i = high; i >= low; i--) { if (prime[i] && (i - 2 >= low && prime[i - 2] == true)) { document.write( "Largest twins in given range: (" + (i - 2) + ", " + (i) + ")" + "</br>" ); twin = true; break; } } if (twin == false) document.write("No such pair exists"); } printTwins(10, 100); </script>
Producción:
Largest twins in given range: (71, 73)
Publicación traducida automáticamente
Artículo escrito por IshitaBhuiya1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA