📌  相关文章
📜  查找给定范围内最大的双胞胎

📅  最后修改于: 2021-04-27 05:53:39             🧑  作者: Mango

给定范围[low..high],在给定范围内(包括低和高)打印最大双胞胎数字。如果两个数字是质数,则它们是双胞胎,并且相差2。

例子:

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)

一个简单的解决方案是从高处开始,对于每个x,检查x和x – 2是否为质数。这里x从高到低+ 2变化。

一个有效的解决方案是使用Eratosthenes筛:

C++
// C++ program to find the largest twin in given range
#include 
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
= $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.
?>


输出:
Largest twins in given range: (71, 73)