给定数字N。任务是计算从2到N的质数个数,该质数可以表示为两个连续质数和1的和。
例子:
Input: N = 27
Output: 2
13 = 5 + 7 + 1 and 19 = 7 + 11 + 1 are the required prime numbers.
Input: N = 34
Output: 3
13 = 5 + 7 + 1, 19 = 7 + 11 + 1 and 31 = 13 + 17 + 1.
方法:一种有效的方法是使用Eratosthenes的Sieve查找直到N的所有素数,并将所有素数放入向量中。现在,运行一个简单的循环并添加两个连续的素数和1,然后检查此和是否也是素数。如果是,则增加计数。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define N 100005
// To check if a number is prime or not
bool isprime[N];
// To store possible numbers
bool can[N];
// Function to return all prime numbers
vector SieveOfEratosthenes()
{
memset(isprime, true, sizeof(isprime));
for (int p = 2; p * p < N; p++) {
// If prime[p] is not changed, then it is a prime
if (isprime[p] == true) {
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (int i = p * p; i < N; i += p)
isprime[i] = false;
}
}
vector primes;
for (int i = 2; i < N; i++)
if (isprime[i])
primes.push_back(i);
return primes;
}
// Function to count all possible prime numbers that can be
// expressed as the sum of two consecutive primes and one
int Prime_Numbers(int n)
{
vector primes = SieveOfEratosthenes();
// All possible prime numbers below N
for (int i = 0; i < (int)(primes.size()) - 1; i++)
if (primes[i] + primes[i + 1] + 1 < N)
can[primes[i] + primes[i + 1] + 1] = true;
int ans = 0;
for (int i = 2; i <= n; i++) {
if (can[i] and isprime[i]) {
ans++;
}
}
return ans;
}
// Driver code
int main()
{
int n = 50;
cout << Prime_Numbers(n);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GfG
{
static int N = 100005;
// To check if a number is prime or not
static boolean isprime[] = new boolean[N];
// To store possible numbers
static boolean can[] = new boolean[N];
// Function to return all prime numbers
static ArrayListSieveOfEratosthenes()
{
for(int a = 0 ; a < isprime.length; a++)
{
isprime[a] = true;
}
for (int p = 2; p * p < N; p++)
{
// If prime[p] is not changed, then it is a prime
if (isprime[p] == true)
{
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (int i = p * p; i < N; i += p)
isprime[i] = false;
}
}
ArrayList primes = new ArrayList ();
for (int i = 2; i < N; i++)
if (isprime[i])
primes.add(i);
return primes;
}
// Function to count all possible prime numbers that can be
// expressed as the sum of two consecutive primes and one
static int Prime_Numbers(int n)
{
ArrayList primes = SieveOfEratosthenes();
// All possible prime numbers below N
for (int i = 0; i < (int)(primes.size()) - 1; i++)
if (primes.get(i) + primes.get(i + 1) + 1 < N)
can[primes.get(i) + primes.get(i + 1) + 1] = true;
int ans = 0;
for (int i = 2; i <= n; i++)
{
if (can[i] && isprime[i] == true)
{
ans++;
}
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int n = 50;
System.out.println(Prime_Numbers(n));
}
}
// This code is contributed by
// Prerna Saini.
Python3
# Python3 implementation of the approach
from math import sqrt;
N = 100005;
# To check if a number is prime or not
isprime = [True] * N;
# To store possible numbers
can = [False] * N;
# Function to return all prime numbers
def SieveOfEratosthenes() :
for p in range(2, int(sqrt(N)) + 1) :
# If prime[p] is not changed,
# then it is a prime
if (isprime[p] == True) :
# Update all multiples of p greater
# than or equal to the square of it
# numbers which are multiple of p and are
# less than p^2 are already been marked.
for i in range(p * p, N , p) :
isprime[i] = False;
primes = [];
for i in range(2, N) :
if (isprime[i]):
primes.append(i);
return primes;
# Function to count all possible prime numbers
# that can be expressed as the sum of two
# consecutive primes and one
def Prime_Numbers(n) :
primes = SieveOfEratosthenes();
# All possible prime numbers below N
for i in range(len(primes) - 1) :
if (primes[i] + primes[i + 1] + 1 < N) :
can[primes[i] + primes[i + 1] + 1] = True;
ans = 0;
for i in range(2, n + 1) :
if (can[i] and isprime[i]) :
ans += 1;
return ans;
# Driver code
if __name__ == "__main__" :
n = 50;
print(Prime_Numbers(n));
# This code is contributed by Ryuga
C#
// C# implementation of the approach
using System;
using System.Collections;
class GfG
{
static int N = 100005;
// To check if a number is prime or not
static bool[] isprime = new bool[N];
// To store possible numbers
static bool[] can = new bool[N];
// Function to return all prime numbers
static ArrayList SieveOfEratosthenes()
{
for(int a = 0 ; a < N; a++)
{
isprime[a] = true;
}
for (int p = 2; p * p < N; p++)
{
// If prime[p] is not changed, then it is a prime
if (isprime[p] == true)
{
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (int i = p * p; i < N; i += p)
isprime[i] = false;
}
}
ArrayList primes = new ArrayList();
for (int i = 2; i < N; i++)
if (isprime[i])
primes.Add(i);
return primes;
}
// Function to count all possible prime numbers that can be
// expressed as the sum of two consecutive primes and one
static int Prime_Numbers(int n)
{
ArrayList primes = SieveOfEratosthenes();
// All possible prime numbers below N
for (int i = 0; i < primes.Count - 1; i++)
if ((int)primes[i] + (int)primes[i + 1] + 1 < N)
can[(int)primes[i] + (int)primes[i + 1] + 1] = true;
int ans = 0;
for (int i = 2; i <= n; i++)
{
if (can[i] && isprime[i] == true)
{
ans++;
}
}
return ans;
}
// Driver code
static void Main()
{
int n = 50;
Console.WriteLine(Prime_Numbers(n));
}
}
// This code is contributed by mits
PHP
输出:
5