📜  小于N的素数计数,可以表示为两个素数之和

📅  最后修改于: 2021-04-21 23:31:58             🧑  作者: Mango

给定整数N ,任务是找到N以下所有素数的计数,可以将其表示为两个素数之和。
例子:

方法:创建一个数组prime [],其中prime [i]将使用Eratosthenes的Sieve存储i是否为质数。现在,对于[1,N – 1]范围内的每个素数,使用此处讨论的方法检查是否可以将其表示为两个素数之和。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
const int MAX = 100005;
bool prime[MAX];
 
// Function for Sieve of Eratosthenes
void SieveOfEratosthenes()
{
    memset(prime, true, sizeof(prime));
 
    // false here indicates
    // that it is not prime
    prime[0] = false;
    prime[1] = false;
 
    for (int p = 2; p * p <= MAX; p++) {
 
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p]) {
 
            // Update all multiples of p,
            // set them to non-prime
            for (int i = p * 2; i <= MAX; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to return the count of primes
// less than or equal to n which can be
// expressed as the sum of two primes
int countPrimes(int n)
{
    SieveOfEratosthenes();
 
    // To store the required count
    int cnt = 0;
 
    for (int i = 2; i < n; i++) {
 
        // If the integer is prime and it
        // can be expressed as the sum of
        // 2 and a prime number
        if (prime[i] && prime[i - 2])
            cnt++;
    }
 
    return cnt;
}
 
// Driver code
int main()
{
    int n = 11;
 
    cout << countPrimes(n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
static int MAX = 100005;
static boolean []prime = new boolean[MAX];
 
// Function for Sieve of Eratosthenes
static void SieveOfEratosthenes()
{
 
    for (int i = 0; i < MAX; i++)
        prime[i] = true;
 
    // false here indicates
    // that it is not prime
    prime[0] = false;
    prime[1] = false;
 
    for (int p = 2; p * p < MAX; p++)
    {
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p])
        {
            // Update all multiples of p,
            // set them to non-prime
            for (int i = p * 2; i < MAX; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to return the count of primes
// less than or equal to n which can be
// expressed as the sum of two primes
static int countPrimes(int n)
{
    SieveOfEratosthenes();
 
    // To store the required count
    int cnt = 0;
 
    for (int i = 2; i < n; i++)
    {
        // If the integer is prime and it
        // can be expressed as the sum of
        // 2 and a prime number
        if (prime[i] && prime[i - 2])
            cnt++;
    }
    return cnt;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 11;
 
    System.out.print(countPrimes(n));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
MAX = 100005
prime = [True for i in range(MAX)]
 
# Function for Sieve of Eratosthenes
def SieveOfEratosthenes():
 
    # False here indicates
    # that it is not prime
    prime[0] = False
    prime[1] = False
 
    for p in range(MAX):
 
        if(p * p > MAX):
            break
 
        # If prime[p] is not changed,
        # then it is a prime
        if (prime[p]):
 
            # Update all multiples of p,
            # set them to non-prime
            for i in range(2 * p, MAX, p):
                prime[i] = False
 
# Function to return the count of primes
# less than or equal to n which can be
# expressed as the sum of two primes
def countPrimes(n):
    SieveOfEratosthenes()
 
    # To store the required count
    cnt = 0
 
    for i in range(2, n):
 
        # If the integer is prime and it
        # can be expressed as the sum of
        # 2 and a prime number
        if (prime[i] and prime[i - 2]):
            cnt += 1
 
    return cnt
 
# Driver code
n = 11
 
print(countPrimes(n))
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
static int MAX = 100005;
static bool []prime = new bool[MAX];
 
// Function for Sieve of Eratosthenes
static void SieveOfEratosthenes()
{
    for (int i = 0; i < MAX; i++)
        prime[i] = true;
 
    // false here indicates
    // that it is not prime
    prime[0] = false;
    prime[1] = false;
 
    for (int p = 2; p * p < MAX; p++)
    {
        // If prime[p] is not changed,
        // then it is a prime
        if (prime[p])
        {
            // Update all multiples of p,
            // set them to non-prime
            for (int i = p * 2; i < MAX; i += p)
                prime[i] = false;
        }
    }
}
 
// Function to return the count of primes
// less than or equal to n which can be
// expressed as the sum of two primes
static int countPrimes(int n)
{
    SieveOfEratosthenes();
 
    // To store the required count
    int cnt = 0;
 
    for (int i = 2; i < n; i++)
    {
        // If the integer is prime and it
        // can be expressed as the sum of
        // 2 and a prime number
        if (prime[i] && prime[i - 2])
            cnt++;
    }
    return cnt;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 11;
 
    Console.Write(countPrimes(n));
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
2