📌  相关文章
📜  计算范围 [L, R] 内不能被前 K 个素数整除的整数

📅  最后修改于: 2022-05-13 01:56:06.526000             🧑  作者: Mango

计算范围 [L, R] 内不能被前 K 个素数整除的整数

给定两个正整数LR ,任务是找出范围[L, R]中不能被前 K 个素数中的任何一个整除的数字的计数。

方法:给定的问题可以使用以下思想来解决:

  • 使用埃拉托色尼筛法将K个素数存储在向量中。
  • 然后对于所有K个素数,迭代一个循环并将L 到 R中该素数的所有倍数标记为可被前K个素数中的任何一个整除。
  • 在此之后迭代一个循环并计算在L 到 R中标记为不可整除的数字。
  • 最后的计数是所需的答案。

下面是上述方法的实现。

C++
// C++ code to implement above approach
#include 
using namespace std;
 
// Code for Sieve of Eratosthenes
// to store first k prime numbers
void SieveOfEratosthenes(int n,
                         vector& v, int k)
{
    bool prime[n + 1];
    memset(prime, true, sizeof(prime));
 
    for (int i = 2; i * i <= n; i++) {
        if (prime[i]) {
            for (int j = i * i; j <= n;
                 j += i)
                prime[j] = false;
        }
    }
 
    // Storing first k prime numbers
    // in vector v.
    for (int i = 2; i < n; i++) {
        if (prime[i]) {
            v.push_back(i);
        }
        if (v.size() == k) {
            break;
        }
    }
}
 
// Function to return the count
// of numbers in range L and R
// which is not divisible by first
// k prime numbers
int total_count(int L, int R, int k)
{
    vector v;
 
    // Calling sieve of eratosthenes
    SieveOfEratosthenes(100000, v, k);
 
    // Initialising the array
    int arr[R + 1] = { 0 };
 
    // Making all the multiples of
    // every prime number in the
    // array arr as 1.
    for (int i = 0; i < v.size(); i++) {
 
        // Val is the first multiple
        // of v[i] which is greater
        // or equal to L.
        int val;
 
        if (L % v[i] == 0) {
            val = L;
        }
        else {
            val = ((L / v[i]) + 1) * v[i];
        }
        for (int j = val; j <= R; j = j
                                      + v[i]) {
            arr[j] = 1;
        }
    }
 
    int count = 0;
    for (int i = L; i <= R; i++) {
        if (arr[i] == 0) {
            count++;
        }
    }
 
    return count;
}
 
// Driver code
int main()
{
    int L = 10, R = 25;
    int K = 3;
 
    // Function call
    cout << total_count(L, R, K);
    return 0;
}


Java
// Java code to implement above approach
import java.io.*;
import java.util.Vector;
 
class GFG {
 
  // Code for Sieve of Eratosthenes
  // to store first k prime numbers
  static void SieveOfEratosthenes(int n,
                                  Vector v, int k)
  {
    boolean[] prime = new boolean[n + 1];
    for(int i = 0; i < n + 1; i++) prime[i] = true;
 
    for (int i = 2; i * i <= n; i++) {
      if (prime[i]) {
        for (int j = i * i; j <= n;
             j += i)
          prime[j] = false;
      }
    }
 
    // Storing first k prime numbers
    // in vector v.
    for (int i = 2; i < n; i++) {
      if (prime[i]) {
        v.add(i);
      }
      if (v.size() == k) {
        break;
      }
    }
  }
 
  // Function to return the count
  // of numbers in range L and R
  // which is not divisible by first
  // k prime numbers
  static int total_count(int L, int R, int k)
  {
    Vector v = new Vector<>();
 
    // Calling sieve of eratosthenes
    SieveOfEratosthenes(100000, v, k);
 
    // Initialising the array
    int[] arr = new int[R + 1];
 
    // Making all the multiples of
    // every prime number in the
    // array arr as 1.
    for (int i = 0; i < v.size(); i++) {
 
      // Val is the first multiple
      // of v[i] which is greater
      // or equal to L.
      int val;
 
      if (L % v.get(i) == 0) {
        val = L;
      }
      else {
        val = ((L / v.get(i)) + 1) * v.get(i);
      }
      for (int j = val; j <= R; j = j
           + v.get(i)) {
        arr[j] = 1;
      }
    }
 
    int count = 0;
    for (int i = L; i <= R; i++) {
      if (arr[i] == 0) {
        count++;
      }
    }
 
    return count;
  }
 
  // Driver code
  public static void main (String[] args) {
    int L = 10, R = 25;
    int K = 3;
 
    // Function call
    System.out.print(total_count(L, R, K));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# python3 code to implement above approach
import math
 
# Code for Sieve of Eratosthenes
# to store first k prime numbers
def SieveOfEratosthenes(n, v, k):
 
    prime = [True for _ in range(n + 1)]
 
    for i in range(2, int(math.sqrt(n)) + 1):
        if (prime[i]):
            for j in range(i*i, n + 1, i):
                prime[j] = False
 
    # Storing first k prime numbers
    # in vector v.
    for i in range(2, n):
        if (prime[i]):
            v.append(i)
 
        if (len(v) == k):
            break
 
# Function to return the count
# of numbers in range L and R
# which is not divisible by first
# k prime numbers
def total_count(L, R, k):
 
    v = []
 
    # Calling sieve of eratosthenes
    SieveOfEratosthenes(100000, v, k)
 
    # Initialising the array
    arr = [0 for _ in range(R + 1)]
 
    # Making all the multiples of
    # every prime number in the
    # array arr as 1.
    for i in range(0, len(v)):
 
        # Val is the first multiple
        # of v[i] which is greater
        # or equal to L.
        val = 0
 
        if (L % v[i] == 0):
            val = L
 
        else:
            val = ((L // v[i]) + 1) * v[i]
 
        for j in range(val, R + 1, v[i]):
            arr[j] = 1
 
    count = 0
    for i in range(L, R+1):
        if (arr[i] == 0):
            count += 1
 
    return count
 
# Driver code
if __name__ == "__main__":
 
    L, R = 10, 25
    K = 3
 
    # Function call
    print(total_count(L, R, K))
 
    # This code is contributed by rakeshsahni


C#
// C# code to implement above approach
using System;
using System.Collections.Generic;
class GFG {
 
  // Code for Sieve of Eratosthenes
  // to store first k prime numbers
  static void SieveOfEratosthenes(int n, List v,
                                  int k)
  {
    bool[] prime = new bool[n + 1];
    for (int i = 0; i < n + 1; i++)
      prime[i] = true;
 
    for (int i = 2; i * i <= n; i++) {
      if (prime[i]) {
        for (int j = i * i; j <= n; j += i)
          prime[j] = false;
      }
    }
 
    // Storing first k prime numbers
    // in vector v.
    for (int i = 2; i < n; i++) {
      if (prime[i]) {
        v.Add(i);
      }
      if (v.Count == k) {
        break;
      }
    }
  }
 
  // Function to return the count
  // of numbers in range L and R
  // which is not divisible by first
  // k prime numbers
  static int total_count(int L, int R, int k)
  {
    List v = new List();
 
    // Calling sieve of eratosthenes
    SieveOfEratosthenes(100000, v, k);
 
    // Initialising the array
    int[] arr = new int[R + 1];
 
    // Making all the multiples of
    // every prime number in the
    // array arr as 1.
    for (int i = 0; i < v.Count; i++) {
 
      // Val is the first multiple
      // of v[i] which is greater
      // or equal to L.
      int val;
 
      if (L % v[i] == 0) {
        val = L;
      }
      else {
        val = ((L / v[i]) + 1) * v[i];
      }
      for (int j = val; j <= R; j = j + v[i]) {
        arr[j] = 1;
      }
    }
 
    int count = 0;
    for (int i = L; i <= R; i++) {
      if (arr[i] == 0) {
        count++;
      }
    }
 
    return count;
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    int L = 10, R = 25;
    int K = 3;
 
    Console.Write(total_count(L, R, K));
  }
}
 
// This code is contributed by ukasp.


Javascript



输出
5

时间复杂度: O(N*log(log(N)) + K*R) 其中 N 是一个高正值
辅助空间: O(N+R)