Find all the prime numbers of given number of digits

Given an integer D, the task is to find all the prime numbers having D digits.

Examples:
Input: D = 1
Output: 2 3 5 7

Input: D = 2
Output: 11 13 17 19 23 29 31 37 41 43 47 53 61 67 71 73 79 83 89 97



Approach: Numbers with D digits lie in the range [10(D – 1), 10D – 1]. So, check all the numbers in this interval and to check the number is prime or not, use Sieve of Eratosthenes to generate all the primes.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Output:

2 3 5 7


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.