📜  特殊素数

📅  最后修改于: 2021-04-27 22:58:33             🧑  作者: Mango

给定两个数字n和k,找出从2到n(含2)之间是否至少存在k个特殊质数。
如果质数可以表示为三个整数(两个相邻质数和1)的总和,则它被称为特殊质数。例如,19 = 7 + 11 + 1或13 = 5 + 7 + 1。
注意:-如果两个素数之间没有其他素数,则称为相邻素数。
例子:

Input : n = 27, k = 2
Output : YES
In this sample the answer is YES 
since at least two numbers are 
Special 13(5 + 7 + 1) and
19(7 + 11 + 1).

Input : n = 45, k = 7
Output : NO
In this example, the Special 
prime numbers are 13(5 + 7 + 1), 
19(7 + 11 + 1), 31(13 + 17 + 1),
37(17 + 19 + 1), 43(19 + 23 + 1).
As the no. of Special prime 
numbers from 2 to 45 is less than
k, the output is NO.

为了解决这个问题,我们需要找到在[2..n]范围内的素数。因此,我们使用Eratosthenes筛子来生成从2到n的所有素数。然后,取每一对相邻的质数,并检查它们的和是否也增加1。计算这些对的数量,将其与K进行比较并输出结果。
下面是上述方法的实现:

C++
// CPP program to check whether there
// exist at least k or not in range [2..n]
#include 
using namespace std;
 
vector primes;
 
// Generating all the prime numbers
// from 2 to n.
void SieveofEratosthenes(int n)
{
    bool visited[n];
    for (int i = 2; i <= n + 1; i++)
        if (!visited[i]) {
            for (int j = i * i; j <= n + 1; j += i)
                visited[j] = true;
            primes.push_back(i);
        }
}
 
bool specialPrimeNumbers(int n, int k)
{
    SieveofEratosthenes(n);
    int count = 0;
    for (int i = 0; i < primes.size(); i++) {
        for (int j = 0; j < i - 1; j++) {
 
            // If a prime number is Special prime
            // number, then we increments the
            // value of k.
            if (primes[j] + primes[j + 1] + 1
                == primes[i]) {
                count++;
                break;
            }
        }
 
        // If at least k Special prime numbers
        // are present, then we return 1.
        // else we return 0 from outside of
        // the outer loop.
        if (count == k)
            return true;
    }
    return false;
}
 
// Driver function
int main()
{
    int n = 27, k = 2;
    if (specialPrimeNumbers(n, k))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
    return 0;
}


Java
// Java program to check whether there
// exist at least k or not in range [2..n]
import java.util.*;
class GFG{
static ArrayList primes = new ArrayList();
// Generating all the prime numbers
// from 2 to n.
static void SieveofEratosthenes(int n)
{
    boolean[] visited=new boolean[n*n+2];
    for (int i = 2; i <= n + 1; i++)
        if (!visited[i]) {
            for (int j = i * i; j <= n + 1; j += i)
                visited[j] = true;
            primes.add(i);
        }
}
 
static boolean specialPrimeNumbers(int n, int k)
{
    SieveofEratosthenes(n);
    int count = 0;
    for (int i = 0; i < primes.size(); i++) {
        for (int j = 0; j < i - 1; j++) {
 
            // If a prime number is Special prime
            // number, then we increments the
            // value of k.
            if (primes.get(j) + primes.get(j + 1) + 1
                == primes.get(i)) {
                count++;
                break;
            }
        }
 
        // If at least k Special prime numbers
        // are present, then we return 1.
        // else we return 0 from outside of
        // the outer loop.
        if (count == k)
            return true;
    }
    return false;
}
 
// Driver function
public static void main(String[] args)
{
    int n = 27, k = 2;
    if (specialPrimeNumbers(n, k))
        System.out.println("YES");
    else
        System.out.println("NO");
}
}
// This code is contributed by mits


Python3
# Python3 program to check whether there
# exist at least k or not in range [2..n]
primes = [];
 
# Generating all the prime numbers
# from 2 to n.
def SieveofEratosthenes(n):
 
    visited = [False] * (n + 2);
    for i in range(2, n + 2):
        if (visited[i] == False):
            for j in range(i * i, n + 2, i):
                visited[j] = True;
            primes.append(i);
 
def specialPrimeNumbers(n, k):
 
    SieveofEratosthenes(n);
    count = 0;
    for i in range(len(primes)):
        for j in range(i - 1):
 
            # If a prime number is Special
            # prime number, then we increments
            # the value of k.
            if (primes[j] +
                primes[j + 1] + 1 == primes[i]):
                count += 1;
                break;
 
        # If at least k Special prime numbers
        # are present, then we return 1.
        # else we return 0 from outside of
        # the outer loop.
        if (count == k):
            return True;
 
    return False;
 
# Driver Code
n = 27;
k = 2;
if (specialPrimeNumbers(n, k)):
    print("YES");
else:
    print("NO");
 
# This code is contributed by mits


C#
// C# program to check whether there
// exist at least k or not in range [2..n]
using System;
using System.Collections;
 
class GFG{
static ArrayList primes = new ArrayList();
// Generating all the prime numbers
// from 2 to n.
static void SieveofEratosthenes(int n)
{
    bool[] visited=new bool[n*n+2];
    for (int i = 2; i <= n + 1; i++)
        if (!visited[i]) {
            for (int j = i * i; j <= n + 1; j += i)
                visited[j] = true;
            primes.Add(i);
        }
}
 
static bool specialPrimeNumbers(int n, int k)
{
    SieveofEratosthenes(n);
    int count = 0;
    for (int i = 0; i < primes.Count; i++) {
        for (int j = 0; j < i - 1; j++) {
 
            // If a prime number is Special prime
            // number, then we increments the
            // value of k.
            if ((int)primes[j] + (int)primes[j + 1] + 1
                == (int)primes[i]) {
                count++;
                break;
            }
        }
 
        // If at least k Special prime numbers
        // are present, then we return 1.
        // else we return 0 from outside of
        // the outer loop.
        if (count == k)
            return true;
    }
    return false;
}
 
// Driver function
public static void Main()
{
    int n = 27, k = 2;
    if (specialPrimeNumbers(n, k))
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
}
// This code is contributed by mits


PHP


Javascript


输出:-

YES