📌  相关文章
📜  计算所有小于10 ^ 6且最小素数为N的数字

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

给定一个数N,它是素数。任务是找到所有小于或等于10 ^ 6的数字,其最小素数为N。

例子:

Input: N = 2
Output: 500000

Input: N = 3
Output: 166667

方法:使用Eratosthenes筛子找到问题的解决方案。存储所有小于10 ^ 6的质数。形成另一个筛子,该筛子将存储最小素数为筛子索引的所有数字的计数。然后显示素数N的计数(即sieve_count [n] +1),其中n是素数。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
#define MAX 1000000
  
// the sieve of prime number and
// count of minimum prime factor
int sieve_Prime[MAX + 4] = { 0 },
                      sieve_count[MAX + 4] = { 0 };
  
// form the prime sieve
void form_sieve()
{
    // 1 is not a prime number
    sieve_Prime[1] = 1;
  
    // form the sieve
    for (int i = 2; i <= MAX; i++) {
  
        // if i is prime
        if (sieve_Prime[i] == 0) {
            for (int j = i * 2; j <= MAX; j += i) {
  
                // if i is the least prime factor
                if (sieve_Prime[j] == 0) {
  
                    // mark the number j as non prime
                    sieve_Prime[j] = 1;
  
                    // count the numbers whose least prime factor is i
                    sieve_count[i]++;
                }
            }
        }
    }
}
  
// Driver code
int main()
{
    // form the sieve
    form_sieve();
  
    int n = 2;
  
    // display
    cout << "Count = " << (sieve_count[n] + 1) << endl;
  
    n = 3;
  
    // display
    cout << "Count = " << (sieve_count[n] + 1) << endl;
  
    return 0;
}


Java
// Java implementation of above approach
import java.io.*;
  
class GFG {
      
static int MAX = 1000000;
  
// the sieve of prime number and
// count of minimum prime factor
static int sieve_Prime[] = new int[MAX + 4];
static int sieve_count[] =  new int[MAX + 4];
  
// form the prime sieve
static void form_sieve()
{
    // 1 is not a prime number
    sieve_Prime[1] = 1;
  
    // form the sieve
    for (int i = 2; i <= MAX; i++) {
  
        // if i is prime
        if (sieve_Prime[i] == 0) {
            for (int j = i * 2; j <= MAX; j += i) {
  
                // if i is the least prime factor
                if (sieve_Prime[j] == 0) {
  
                    // mark the number j as non prime
                    sieve_Prime[j] = 1;
  
                    // count the numbers whose least prime factor is i
                    sieve_count[i]++;
                }
            }
        }
    }
}
  
// Driver code
  
    public static void main (String[] args) {
        // form the sieve
    form_sieve();
  
    int n = 2;
  
    // display
    System.out.println( "Count = " + (sieve_count[n] + 1));
  
    n = 3;
  
    // display
    System.out.println ("Count = "  +(sieve_count[n] + 1));
    }
}
// This code was contributed
// by inder_mca


Python3
# Python3 implementation of
# above approach
  
MAX = 1000000
  
# the sieve of prime number and
# count of minimum prime factor
sieve_Prime = [0 for i in range(MAX + 4)]
sieve_count = [0 for i in range(MAX + 4)]
  
# form the prime sieve
def form_sieve():
      
    # 1 is not a prime number
    sieve_Prime[1] = 1
  
    # form the sieve
    for i in range(2, MAX + 1):
  
        # if i is prime
        if sieve_Prime[i] == 0:
            for j in range(i * 2, MAX + 1, i):
  
                # if i is the least prime factor
                if sieve_Prime[j] == 0:
  
                    # mark the number j 
                    # as non prime
                    sieve_Prime[j] = 1
  
                    # count the numbers whose 
                    # least prime factor is i
                    sieve_count[i] += 1
  
# Driver code
  
# form the sieve
form_sieve()
  
n = 2
  
# display
print("Count =", sieve_count[n] + 1)
  
n = 3
  
# display
print("Count =", sieve_count[n] + 1)
  
# This code was contributed
# by VishalBachchas


C#
// C# implementation of above approach
using System;
  
class GFG {
      
static int MAX = 1000000;
  
// the sieve of prime number and
// count of minimum prime factor
static int []sieve_Prime = new int[MAX + 4];
static int []sieve_count = new int[MAX + 4];
  
// form the prime sieve
static void form_sieve()
{
    // 1 is not a prime number
    sieve_Prime[1] = 1;
  
    // form the sieve
    for (int i = 2; i <= MAX; i++) {
  
        // if i is prime
        if (sieve_Prime[i] == 0) {
            for (int j = i * 2; j <= MAX; j += i) {
  
                // if i is the least prime factor
                if (sieve_Prime[j] == 0) {
  
                    // mark the number j as non prime
                    sieve_Prime[j] = 1;
  
                    // count the numbers whose least prime factor is i
                    sieve_count[i]++;
                }
            }
        }
    }
}
  
// Driver code
  
    public static void Main () {
        // form the sieve
    form_sieve();
  
    int n = 2;
  
    // display
    Console.WriteLine( "Count = " + (sieve_count[n] + 1));
  
    n = 3;
  
    // display
    Console.WriteLine ("Count = " +(sieve_count[n] + 1));
    }
}
// This code was contributed
// by shs


PHP


输出:
Count = 500000
Count = 166667