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

📅  最后修改于: 2021-05-04 14:54:19             🧑  作者: Mango

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

例子:

Input:  low = 10,  high = 100
Output: Smallest twins in given range: (11, 13)
Both 11 and 13 are prime numbers and difference 
between them is two, therefore twins.  And these
are the smallest twins in [10..100]

Input:  low = 50,  high = 100
Output: Smallest twins in given range: (59, 61) 

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

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

1) Create a boolean array "prime[0..high]" and initialize all 
   entries in it as true. A value in prime[i] will finally 
   be false if i is not a prime number, else true.

2) Run a loop from p = 2 to high. 
    a) If prime[p] is true, then p is prime. [See this]
    b) Mark all multiples of p as not prime in prime[]. 

3) Run a loop from low to high and print the first twins
   using prime[] built in step 2.   

以下是上述想法的实现。

C++
// C++ program to find the smallest twin in given range
#include 
using namespace std;
 
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 smallest twin in range
    for (int i=low; i<=high; i++)
    {
        if (prime[i] && prime[i+2])
        {
            cout << "Smallest twins in given range: ("
                << i << ", " << i+2 << ")";
            twin = true;
            break;
        }
    }
     
    if (twin == false)
      cout << "No such pair exists" <


Java
// Java program to find the smallest twin in given range
 
class GFG {
 
    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], twin = false;
        for (int i = 0; i < prime.length; 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 smallest twin in range
        for (int i = low; i <= high; i++) {
            if (prime[i] && prime[i + 2]) {
                int a = i + 2 ;
                System.out.print("Smallest twins in given range: ("
                        + i + ", " + a + ")");
                twin = true;
                break;
            }
        }
 
        if (twin == false) {
            System.out.println("No such pair exists");
        }
    }
 
// Driver program
    public static void main(String[] args) {
 
        printTwins(10, 100);
    }
}
// This code contributed by Rajput-Ji


Python3
# Python3 program to find the smallest
# twin in given range
import math
 
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] * (high + 1);
    twin = False;
 
    prime[0] = prime[1] = False;
 
    # Look for the smallest twin
    for p in range(2, int(math.floor(
                          math.sqrt(high)) + 2)):
         
        # 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 smallest twin in range
    for i in range(low, high + 1):
        if (prime[i] and prime[i + 2]):
            print("Smallest twins in given range: (",
                               i, ",", (i + 2), ")");
            twin = True;
            break;
     
    if (twin == False):
        print("No such pair exists");
 
# Driver Code
printTwins(10, 100);
     
# This code is contributed
# by chandan_jnu


C#
// C# program to find the smallest twin in given range
 
using System;
public class GFG {
  
    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 < prime.Length; 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 smallest twin in range
        for (int i = low; i <= high; i++) {
            if (prime[i] && prime[i + 2]) {
                int a = i + 2 ;
                Console.Write("Smallest twins in given range: ("
                        + i + ", " + a + ")");
                twin = true;
                break;
            }
        }
  
        if (twin == false) {
            Console.WriteLine("No such pair exists");
        }
    }
  
// Driver program
    public static void Main() {
  
        printTwins(10, 100);
    }
}
//this code contributed by Rajput-Ji


PHP


Javascript


输出:
Smallest twins in given range: (11, 13)