📜  给定范围 L 到 R 中双质数的计数

📅  最后修改于: 2021-09-03 14:59:58             🧑  作者: Mango

给定两个整数 L 和 R ,任务是在范围内找到双质数的个数。

例子:

方法:
为了解决上面提到的问题,我们将使用 Sieve 的概念来生成素数。

  • 生成 0 到 10 6 的所有质数并存储在数组中。
  • 初始化一个变量count以跟踪从 1 到第 i 个位置的素数。
  • 然后对于每个素数,我们将增加计数并设置 dp[count] = 1(其中 dp 是存储双素数的数组)指示从 1 到第 i 个位置的素数的数量。
  • 最后,找到 dp 数组的累积和,所以答案将是dp[R] – dp[L – 1]

下面是上述方法的实现:

C++
// C++ program to find the count
// of Double Prime numbers
// in the range L to R
 
#include 
using namespace std;
 
// Array to make Sieve
// where arr[i]=0 indicates
// non prime and arr[i] = 1
// indicates prime
int arr[1000001];
 
// Array to find double prime
int dp[1000001];
 
// Function to find the number
// double prime numbers in range
void count()
{
    int maxN = 1000000, i, j;
 
    // Assume all numbers as prime
    for (i = 0; i < maxN; i++)
        arr[i] = 1;
 
    arr[0] = 0;
    arr[1] = 0;
 
    for (i = 2; i * i <= maxN; i++) {
 
        // Check if the number is prime
        if (arr[i] == 1) {
 
            // check for multiples of i
            for (j = 2 * i; j <= maxN; j += i) {
 
                // Make all multiples of
                // ith prime as non-prime
                arr[j] = 0;
            }
        }
    }
 
    int cnt = 0;
 
    for (i = 0; i <= maxN; i++) {
        // Check if number at ith position
        // is prime then increment count
        if (arr[i] == 1)
            cnt++;
 
        if (arr[cnt] == 1)
 
            // Indicates count of numbers
            // from 1 to i that are
            // also prime and
            // hence double prime
            dp[i] = 1;
 
        else
            // If number is not a double prime
            dp[i] = 0;
    }
    for (i = 1; i <= maxN; i++)
        // finding cumulative sum
        dp[i] += dp[i - 1];
}
 
// Driver code
int main()
{
    int L = 4, R = 12;
    count();
    cout << dp[R] - dp[L - 1];
 
    return 0;
}


Java
// Java program to find the count
// of Double Prime numbers
// in the range L to R
import java.util.*;
import java.lang.*;
class GFG{
     
// Array to make Sieve
// where arr[i]=0 indicates
// non prime and arr[i] = 1
// indicates prime
static int[] arr = new int[1000001];
 
// Array to find double prime
static int[] dp = new int[1000001];
 
// Function to find the number
// double prime numbers in range
static void count()
{
    int maxN = 1000000, i, j;
 
    // Assume all numbers as prime
    for (i = 0; i < maxN; i++)
        arr[i] = 1;
 
    arr[0] = 0;
    arr[1] = 0;
 
    for (i = 2; i * i <= maxN; i++)
    {
 
        // Check if the number is prime
        if (arr[i] == 1)
        {
 
            // check for multiples of i
            for (j = 2 * i; j <= maxN; j += i)
            {
 
                // Make all multiples of
                // ith prime as non-prime
                arr[j] = 0;
            }
        }
    }
 
    int cnt = 0;
 
    for (i = 0; i <= maxN; i++)
    {
        // Check if number at ith position
        // is prime then increment count
        if (arr[i] == 1)
            cnt++;
 
        if (arr[cnt] == 1)
 
            // Indicates count of numbers
            // from 1 to i that are
            // also prime and
            // hence double prime
            dp[i] = 1;
 
        else
            // If number is not a double prime
            dp[i] = 0;
    }
     
    for (i = 1; i <= maxN; i++)
     
        // finding cumulative sum
        dp[i] += dp[i - 1];
}
 
// Driver code
public static void main(String[] args)
{
    int L = 4, R = 12;
    count();
    System.out.println(dp[R] - dp[L - 1]);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to find the count
# of Double Prime numbers
# in the range L to R
 
# Array to make Sieve
# where arr[i]=0 indicates
# non prime and arr[i] = 1
# indicates prime
arr = [0] * 1000001
 
# Array to find double prime
dp = [0] * 1000001
 
# Function to find the number
# double prime numbers in range
def count():
     
    maxN = 1000000
 
    # Assume all numbers as prime
    for i in range(0, maxN):
        arr[i] = 1
 
    arr[0] = 0
    arr[1] = 0
     
    i = 2
    while(i * i <= maxN):
 
        # Check if the number is prime
        if (arr[i] == 1):
 
            # Check for multiples of i
            for j in range(2 * i, maxN + 1, i):
 
                # Make all multiples of
                # ith prime as non-prime
                arr[j] = 0
                 
        i += 1
 
    cnt = 0
 
    for i in range(0, maxN + 1):
         
        # Check if number at ith position
        # is prime then increment count
        if (arr[i] == 1):
            cnt += 1
 
        if (arr[cnt] == 1):
 
            # Indicates count of numbers
            # from 1 to i that are
            # also prime and
            # hence double prime
            dp[i] = 1
 
        else:
             
            # If number is not a double prime
            dp[i] = 0
     
    for i in range(0, maxN + 1):
         
        # Finding cumulative sum
        dp[i] += dp[i - 1]
 
# Driver code
L = 4
R = 12
     
count()
 
print(dp[R] - dp[L - 1])
 
# This code is contributed by sanjoy_62


C#
// C# program to find the count
// of Double Prime numbers
// in the range L to R
using System;
class GFG{
     
// Array to make Sieve
// where arr[i]=0 indicates
// non prime and arr[i] = 1
// indicates prime
static int[] arr = new int[1000001];
 
// Array to find double prime
static int[] dp = new int[1000001];
 
// Function to find the number
// double prime numbers in range
static void count()
{
    int maxN = 1000000, i, j;
 
    // Assume all numbers as prime
    for (i = 0; i < maxN; i++)
        arr[i] = 1;
 
    arr[0] = 0;
    arr[1] = 0;
 
    for (i = 2; i * i <= maxN; i++)
    {
 
        // Check if the number is prime
        if (arr[i] == 1)
        {
 
            // check for multiples of i
            for (j = 2 * i; j <= maxN; j += i)
            {
 
                // Make all multiples of
                // ith prime as non-prime
                arr[j] = 0;
            }
        }
    }
 
    int cnt = 0;
 
    for (i = 0; i <= maxN; i++)
    {
        // Check if number at ith position
        // is prime then increment count
        if (arr[i] == 1)
            cnt++;
 
        if (arr[cnt] == 1)
 
            // Indicates count of numbers
            // from 1 to i that are
            // also prime and
            // hence double prime
            dp[i] = 1;
 
        else
            // If number is not a double prime
            dp[i] = 0;
    }
     
    for (i = 1; i <= maxN; i++)
     
        // finding cumulative sum
        dp[i] += dp[i - 1];
}
 
// Driver code
public static void Main()
{
    int L = 4, R = 12;
    count();
    Console.Write(dp[R] - dp[L - 1]);
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
5