Find all the prime numbers of given number of digits

07 Mar, 2022
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++
Java
Python 3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; const int sz = 1e5;bool isPrime[sz + 1]; // Function for Sieve of Eratosthenesvoid 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 digitsvoid 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 codeint main(){     // Generate primes    sieve();    int d = 1;    findPrimesD(d);     return 0;}
// Java implementation of the approachimport java.util.*; class GFG{static int sz = 100000;static boolean isPrime[] = new boolean[sz + 1]; // Function for Sieve of Eratosthenesstatic 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 digitsstatic 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 codepublic static void main(String args[]){     // Generate primes    sieve();    int d = 1;    findPrimesD(d);}} // This code is contributed by Arnab Kundu
# Python 3 implementation of the approachfrom math import sqrt, powsz = 100005isPrime = [True for i in range(sz + 1)] # Function for Sieve of Eratosthenesdef 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 digitsdef 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 codeif __name__ == '__main__':         # Generate primes    sieve()    d = 1    findPrimesD(d)     # This code is contributed by Surendra_Gangwar
// C# implementation of the approachusing System; class GFG{     static int sz = 100000;static bool []isPrime = new bool[sz + 1]; // Function for Sieve of Eratosthenesstatic 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 digitsstatic 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 codestatic public void Main (){         // Generate primes    sieve();    int d = 1;    findPrimesD(d); }} // This code is contributed by ajit.
<script>    // Javascript implementation of the approach         let sz = 100000;    let isPrime = new Array(sz + 1);    isPrime.fill(false);     // Function for Sieve of Eratosthenes    function sieve()    {        for(let i = 0; i <= sz; i++)            isPrime[i] = true;         isPrime[0] = isPrime[1] = false;         for (let i = 2; i * i <= sz; i++)        {            if (isPrime[i])            {                for (let j = i * i; j < sz; j += i)                {                    isPrime[j] = false;                }            }        }    }     // Function to print all the prime    // numbers with d digits    function findPrimesD(d)    {         // Range to check integers        let left = Math.pow(10, d - 1);        let right = Math.pow(10, d) - 1;         // For every integer in the range        for (let i = left; i <= right; i++)        {             // If the current integer is prime            if (isPrime[i])            {                document.write(i + " ");            }        }    }         // Generate primes    sieve();    let d = 1;    findPrimesD(d); // This code is contributed by suresh07.</script>
2 3 5 7
 
Time Complexity: O(sqrt(105) + d)
Auxiliary Space: O(105)
andrew1234
SURENDRA_GANGWAR
jit_t
suresh07
subham348
Prime Number
Mathematical
Mathematical
Prime Number
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Find minimum number of coins that make a given value
Modulo 10^9+7 (1000000007)
Minimum number of jumps to reach end
Algorithm to solve Rubik's Cube
The Knight's tour problem | Backtracking-1
Program for Decimal to Binary Conversion
Modulo Operator (%) in C/C++ with Examples
Program for factorial of a number