📌  相关文章
📜  查找给定数字位数的所有质数

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

给定整数D ,任务是查找所有具有D位的质数。

方法:D位数的数字在[10 (D – 1) ,10 D – 1]范围内。因此,检查此间隔中的所有数字,并检查数字是否为质数,请使用Eratosthenes筛网生成所有质数。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
const int sz = 1e5;
bool isPrime[sz + 1];
  
// Function for Sieve of Eratosthenes
void sieve()
{
    memset(isPrime, true, sizeof(isPrime));
  
    isPrime[0] = isPrime[1] = false;
  
    for (int i = 2; i * i <= sz; i++) {
        if (isPrime[i]) {
            for (int j = i * i; j < sz; j += i) {
                isPrime[j] = false;
            }
        }
    }
}
  
// Function to print all the prime
// numbers with d digits
void findPrimesD(int d)
{
  
    // Range to check integers
    int left = pow(10, d - 1);
    int right = pow(10, d) - 1;
  
    // For every integer in the range
    for (int i = left; i <= right; i++) {
  
        // If the current integer is prime
        if (isPrime[i]) {
            cout << i << " ";
        }
    }
}
  
// Driver code
int main()
{
  
    // Generate primes
    sieve();
    int d = 1;
    findPrimesD(d);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG
{
static int sz = 100000;
static boolean isPrime[] = new boolean[sz + 1];
  
// Function for Sieve of Eratosthenes
static void sieve()
{
    for(int i = 0; i <= sz; i++)
    isPrime[i] = true;
      
    isPrime[0] = isPrime[1] = false;
  
    for (int i = 2; i * i <= sz; i++) 
    {
        if (isPrime[i]) 
        {
            for (int j = i * i; j < sz; j += i) 
            {
                isPrime[j] = false;
            }
        }
    }
}
  
// Function to print all the prime
// numbers with d digits
static void findPrimesD(int d)
{
  
    // Range to check integers
    int left = (int)Math.pow(10, d - 1);
    int right = (int)Math.pow(10, d) - 1;
  
    // For every integer in the range
    for (int i = left; i <= right; i++)
    {
  
        // If the current integer is prime
        if (isPrime[i]) 
        {
            System.out.print(i + " ");
        }
    }
}
  
// Driver code
public static void main(String args[])
{
  
    // Generate primes
    sieve();
    int d = 1;
    findPrimesD(d);
}
}
  
// This code is contributed by Arnab Kundu


Python 3
# Python 3 implementation of the approach
from math import sqrt, pow
sz = 100005
isPrime = [True for i in range(sz + 1)]
  
# Function for Sieve of Eratosthenes
def sieve():
    isPrime[0] = isPrime[1] = False
  
    for i in range(2, int(sqrt(sz)) + 1, 1):
        if (isPrime[i]):
            for j in range(i * i, sz, i):
                isPrime[j] = False
  
# Function to print all the prime
# numbers with d digits
def findPrimesD(d):
      
    # Range to check integers
    left = int(pow(10, d - 1))
    right = int(pow(10, d) - 1)
  
    # For every integer in the range
    for i in range(left, right + 1, 1):
          
        # If the current integer is prime
        if (isPrime[i]):
            print(i, end = " ")
          
# Driver code
if __name__ == '__main__':
      
    # Generate primes
    sieve()
    d = 1
    findPrimesD(d)
      
# This code is contributed by Surendra_Gangwar


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
static int sz = 100000;
static bool []isPrime = new bool[sz + 1];
  
// Function for Sieve of Eratosthenes
static void sieve()
{
    for(int i = 0; i <= sz; i++)
    isPrime[i] = true;
      
    isPrime[0] = isPrime[1] = false;
  
    for (int i = 2; i * i <= sz; i++) 
    {
        if (isPrime[i]) 
        {
            for (int j = i * i; j < sz; j += i) 
            {
                isPrime[j] = false;
            }
        }
    }
}
  
// Function to print all the prime
// numbers with d digits
static void findPrimesD(int d)
{
  
    // Range to check integers
    int left = (int)Math.Pow(10, d - 1);
    int right = (int)Math.Pow(10, d) - 1;
  
    // For every integer in the range
    for (int i = left; i <= right; i++)
    {
  
        // If the current integer is prime
        if (isPrime[i]) 
        {
            Console.Write(i + " ");
        }
    }
}
  
// Driver code
static public void Main ()
{
      
    // Generate primes
    sieve();
    int d = 1;
    findPrimesD(d);
  
}
}
  
// This code is contributed by ajit.


输出:
2 3 5 7