📜  查询给定范围内的合成数和素数之间的差

📅  最后修改于: 2021-04-23 16:13:44             🧑  作者: Mango

给定Q个查询,其中每个查询由两个正整数LR组成,任务是查找素数和复合数之间的绝对差,范围为[L,R]

例子:

方法:

  • 使用Eratosthenes筛,生成一个数组prime [i] ,如果i质数,则prime [i] = 1 ,否则为0
  • 现在更新素[]数组,使得素[I]存储其是≤我素数的计数。
  • 对于每个查询,可以通过prime [R] – prime [L – 1]找出[L,R]范围内的质数计数,而复合数的计数将是从总数中减去的质数的计数元素。
  • 打印在上一步中发现的质数和复合物数之间的绝对差。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define MAX 1000000
int prime[MAX + 1];
  
// Function to update prime[]
// such prime[i] stores the
// count of prime numbers <= i
void updatePrimes()
{
    // prime[] marks all prime numbers as true
    // so prime[i] = 1 if ith number is a prime
  
    // Initialization
    for (int i = 2; i <= MAX; i++) {
        prime[i] = 1;
    }
  
    // 0 and 1 are not primes
    prime[0] = prime[1] = 0;
  
    // Mark composite numbers as false
    // and prime numbers as true
    for (int i = 2; i * i <= MAX; i++) {
        if (prime[i] == 1) {
            for (int j = i * i; j <= MAX; j += i) {
                prime[j] = 0;
            }
        }
    }
  
    // Update prime[] such that
    // prime[i] will store the count of
    // all the prime numbers <= i
    for (int i = 1; i <= MAX; i++) {
        prime[i] += prime[i - 1];
    }
}
  
// Function to return the absolute difference
// between the number of primes and the number
// of composite numbers in the range [l, r]
int getDifference(int l, int r)
{
  
    // Total elements in the range
    int total = r - l + 1;
  
    // Count of primes in the range [l, r]
    int primes = prime[r] - prime[l - 1];
  
    // Count of composite numbers
    // in the range [l, r]
    int composites = total - primes;
  
    // Return the sbsolute difference
    return (abs(primes - composites));
}
  
// Driver code
int main()
{
    int queries[][2] = { { 1, 10 }, { 5, 30 } };
    int q = sizeof(queries) / sizeof(queries[0]);
  
    updatePrimes();
  
    // Perform queries
    for (int i = 0; i < q; i++)
        cout << getDifference(queries[i][0],
                              queries[i][1])
             << endl;
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
    static int MAX = 1000000;
    static int []prime = new int[MAX + 1];
  
    // Function to update prime[]
    // such prime[i] stores the
    // count of prime numbers <= i
    static void updatePrimes()
    {
        // prime[] marks all prime numbers as true
        // so prime[i] = 1 if ith number is a prime
      
        // Initialization
        for (int i = 2; i <= MAX; i++) 
        {
            prime[i] = 1;
        }
      
        // 0 and 1 are not primes
        prime[0] = prime[1] = 0;
      
        // Mark composite numbers as false
        // and prime numbers as true
        for (int i = 2; i * i <= MAX; i++) 
        {
            if (prime[i] == 1) 
            {
                for (int j = i * i; j <= MAX; j += i) 
                {
                    prime[j] = 0;
                }
            }
        }
  
        // Update prime[] such that
        // prime[i] will store the count of
        // all the prime numbers <= i
        for (int i = 1; i <= MAX; i++) 
        {
            prime[i] += prime[i - 1];
        }
    }
  
    // Function to return the absolute difference
    // between the number of primes and the number
    // of composite numbers in the range [l, r]
    static int getDifference(int l, int r)
    {
      
        // Total elements in the range
        int total = r - l + 1;
      
        // Count of primes in the range [l, r]
        int primes = prime[r] - prime[l - 1];
      
        // Count of composite numbers
        // in the range [l, r]
        int composites = total - primes;
      
        // Return the sbsolute difference
        return (Math.abs(primes - composites));
    }
  
    // Driver code
    public static void main (String[] args) 
    {
  
        int queries[][] = { { 1, 10 }, { 5, 30 } };
        int q = queries.length;
        updatePrimes();
          
        // Perform queries
        for (int i = 0; i < q; i++)
            System.out.println (getDifference(queries[i][0],
                                queries[i][1]));
  
    }
}
  
// This code is contributed by jit_t


Python3
# Python3 implementation of the approach 
from math import sqrt 
  
MAX = 1000000
prime = [0]*(MAX + 1); 
  
# Function to update prime[] 
# such prime[i] stores the 
# count of prime numbers <= i 
def updatePrimes() :
  
    # prime[] marks all prime numbers as true 
    # so prime[i] = 1 if ith number is a prime 
  
    # Initialization 
    for i in range(2, MAX + 1) : 
        prime[i] = 1; 
  
    # 0 and 1 are not primes 
    prime[0] = prime[1] = 0; 
  
    # Mark composite numbers as false 
    # and prime numbers as true 
    for i in range(2, int(sqrt(MAX) + 1)) :
        if (prime[i] == 1) :
            for j in range(i*i, MAX, i) : 
                prime[j] = 0; 
  
    # Update prime[] such that 
    # prime[i] will store the count of 
    # all the prime numbers <= i 
    for i in range(1, MAX) :
        prime[i] += prime[i - 1]; 
  
# Function to return the absolute difference 
# between the number of primes and the number 
# of composite numbers in the range [l, r] 
def getDifference(l, r) :
  
    # Total elements in the range 
    total = r - l + 1; 
  
    # Count of primes in the range [l, r] 
    primes = prime[r] - prime[l - 1]; 
  
    # Count of composite numbers 
    # in the range [l, r] 
    composites = total - primes; 
  
    # Return the sbsolute difference 
    return (abs(primes - composites)); 
  
  
# Driver code 
if __name__ == "__main__" : 
  
    queries = [ [ 1, 10 ],[ 5, 30 ] ]; 
    q = len(queries); 
  
    updatePrimes(); 
  
    # Perform queries 
    for i in range(q) :
        print(getDifference(queries[i][0], 
                            queries[i][1]))
              
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
      
class GFG 
{
    static int MAX = 1000000;
    static int []prime = new int[MAX + 1];
  
    // Function to update prime[]
    // such prime[i] stores the
    // count of prime numbers <= i
    static void updatePrimes()
    {
        // prime[] marks all prime numbers as true
        // so prime[i] = 1 if ith number is a prime
      
        // Initialization
        for (int i = 2; i <= MAX; i++) 
        {
            prime[i] = 1;
        }
      
        // 0 and 1 are not primes
        prime[0] = prime[1] = 0;
      
        // Mark composite numbers as false
        // and prime numbers as true
        for (int i = 2; i * i <= MAX; i++) 
        {
            if (prime[i] == 1) 
            {
                for (int j = i * i; j <= MAX; j += i) 
                {
                    prime[j] = 0;
                }
            }
        }
  
        // Update prime[] such that
        // prime[i] will store the count of
        // all the prime numbers <= i
        for (int i = 1; i <= MAX; i++) 
        {
            prime[i] += prime[i - 1];
        }
    }
  
    // Function to return the absolute difference
    // between the number of primes and the number
    // of composite numbers in the range [l, r]
    static int getDifference(int l, int r)
    {
      
        // Total elements in the range
        int total = r - l + 1;
      
        // Count of primes in the range [l, r]
        int primes = prime[r] - prime[l - 1];
      
        // Count of composite numbers
        // in the range [l, r]
        int composites = total - primes;
      
        // Return the sbsolute difference
        return (Math.Abs(primes - composites));
    }
  
    // Driver code
    public static void Main () 
    {
  
        int [,]queries = { { 1, 10 }, { 5, 30 } };
        int q = queries.GetLength(0);
        updatePrimes();
          
        // Perform queries
        for (int i = 0; i < q; i++)
            Console.WriteLine(getDifference(queries[i,0],
                                queries[i,1]));
  
    }
}
  
/* This code contributed by PrinciRaj1992 */


输出:
2
10