The Wayback Machine - https://web.archive.org/web/20240914090340/https://www.geeksforgeeks.org/find-all-the-prime-numbers-of-given-number-of-digits/
Open In App

Find all the prime numbers of given number of digits

Last Updated : 07 Mar, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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




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


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.


Javascript




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


Output: 

2 3 5 7

 

Time Complexity: O(sqrt(105) + d)

Auxiliary Space: O(105)



Similar Reads

Count numbers in a given range having prime and non-prime digits at prime and non-prime positions respectively
Given two integers L and R, the task is to find the count of numbers in the range [L, R] having prime digits at prime positions and non-prime digits at non-prime positions. Examples: Input: L = 5, R = 22 Output: 7Explanation: The numbers 6, 8, 9, 12, 13, 15, and 17 have prime digits at prime positions and non-prime digits at non-prime positions. In
15 min read
Numbers with sum of digits equal to the sum of digits of its all prime factor
Given a range, the task is to find the count of the numbers in the given range such that the sum of its digit is equal to the sum of all its prime factors digits sum.Examples: Input: l = 2, r = 10 Output: 5 2, 3, 4, 5 and 7 are such numbers Input: l = 15, r = 22 Output: 3 17, 19 and 22 are such numbers As, 17 and 19 are already prime. Prime Factors
13 min read
Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers
Given a number N (N &gt; 6), the task is to print the prime factorization of a number Z, where Z is the product of all numbers ≤ N that are even and can be expressed as the product of two distinct prime numbers. Example: Input: N = 6Output: 2→1 3→1Explanation: 6 is the only number ≤ N, which is even and a product of two distinct prime numbers (2 an
7 min read
Find smallest number with given number of digits and sum of digits under given constraints
Given two integers S and D, the task is to find the number having D number of digits and the sum of its digits as S such that the difference between the maximum and the minimum digit in the number is as minimum as possible. If multiple such numbers are possible, print the smallest number.Examples: Input: S = 25, D = 4 Output: 6667 The difference be
7 min read
Count all prime numbers that can be formed using digits of a given number
Given a string S consisting of N digits, the task is to find the number of distinct Prime Numbers that can be formed using the digits of the string S. Examples: Input: S = "123"Output: 5Explanation:The prime numbers that can be formed from the digits of the string S is 2, 3, 13, 23, and 31. Hence, the total count is 5. Input: S = "1"Output: 0 Appro
9 min read
Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
Given an integer N, the task is to print N integers ? 109 such that no two consecutive of these integers are co-prime and every 3 consecutive are co-prime. Examples: Input: N = 3 Output: 6 15 10Input: N = 4 Output: 6 15 35 14 Approach: We can just multiply consecutive primes and for the last number just multiply the gcd(last, last-1) * 2. We do thi
15+ min read
Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
Given three positive integers N, A, and B. The task is to count the numbers of length N containing only digits A and B and whose sum of digits also contains the digits A and B only. Print the answer modulo 109 + 7.Examples: Input: N = 3, A = 1, B = 3 Output: 1 Possible numbers of length 3 are 113, 131, 111, 333, 311, 331 and so on... But only 111 i
15 min read
Count N-digits numbers made up of even and prime digits at odd and even positions respectively
Given a positive integer N, the task is to find the number of integers of N digits having even digits at odd indices and prime digits at even indices. Examples: Input: N = 2Output: 20Explanation:Following are the possible number of 2-digits satisfying the given criteria {20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 50, 52, 54, 56, 58, 70, 72, 74, 76, 78
12 min read
Print prime numbers with prime sum of digits in an array
Given an array arr[] and the task is to print the additive primes in an array. Additive primes: Primes such that the sum of their digits is also a prime, such as 2, 3, 7, 11, 23 are additive primes but not 13, 19, 31 etc.Examples: Input: arr[] = {2, 4, 6, 11, 12, 18, 7} Output: 2, 11, 7 Input: arr[] = {2, 3, 19, 13, 25, 7} Output: 2, 3, 7 A simple
8 min read
Sum of prime numbers without odd prime digits
Given an integer N. The task is to find the sum of the first N prime numbers which don't contain any odd primes as their digit.Some of such prime numbers are 2, 11, 19, 29, 41 ...... Examples: Input : N = 2 Output : 13 2 + 11 = 13Input : N = 7 Output : 252 Approach : We first use a Sieve of Eratosthenes to store all prime numbers.Next check for eac
8 min read
Minimum digits to be removed to make either all digits or alternating digits same
Given a numeric string str, the task is to find the minimum number of digits to be removed from the string such that it satisfies either of the below conditions: All the elements of the string are the same.All the elements at even position are same and all the elements at the odd position are same, which means the string is alternating with the equ
7 min read
Find smallest number with given number of digits and sum of digits
How to find the smallest number with given digit sum s and number of digits d? Examples : Input : s = 9, d = 2 Output : 18 There are many other possible numbers like 45, 54, 90, etc with sum of digits as 9 and number of digits as 2. The smallest of them is 18. Input : s = 20, d = 3 Output : 299 Recommended PracticeSmallest numberTry It! A Simple So
9 min read
Find the Largest number with given number of digits and sum of digits
Given an integer s and d, The task is to find the largest number with given digit sum s and the number of digits d. Examples: Input: s = 9, d = 2Output: 90 Input: s = 20, d = 3Output: 992 Recommended PracticeLargest number possibleTry It! Naive Approach: Consider all m digit numbers and keep a max variable to store the maximum number with m digits
13 min read
Maximize difference between sum of prime and non-prime array elements by left shifting of digits minimum number of times
Given an array arr[] of size N, the task is to find the maximum difference between the sum of the prime numbers and the sum of the non-prime numbers present in the array, by left shifting the digits of array elements by 1 minimum number of times. Examples: Input: arr[] = {541, 763, 321, 716, 143}Output: Difference = 631, Count of operations = 6Expl
14 min read
Cpp14 Program to Maximize difference between sum of prime and non-prime array elements by left shifting of digits minimum number of times
Given an array arr[] of size N, the task is to find the maximum difference between the sum of the prime numbers and the sum of the non-prime numbers present in the array, by left shifting the digits of array elements by 1 minimum number of times. Examples: Input: arr[] = {541, 763, 321, 716, 143}Output: Difference = 631, Count of operations = 6Expl
5 min read
Count of unique prime numbers formed by deleting digits of given number
Given a number N, the task is to count the number of unique prime numbers that can be formed by deleting zero or more digits of the given number. Examples: Input: N = 132Output: 3Explanation: The total prime numbers formed by deleting zero or more digits of the given number 132 are 3 i.e. [3, 2, 13]. Input: N = 2222Output: 1 Approach: This problem
8 min read
Count numbers in a given range whose count of prime factors is a Prime Number
Given a 2D array Q[][] of size N * 2 representing queries of the form {L, R}. For each query, the task is to print the count of numbers in the range [L, R] with a count of prime factors equal to a prime number. Examples: Input: Q[][] = {{4, 8}, {30, 32}} Output: 3 2 Explanation: Query 1: Prime factors of 4 = {2, 2} and count of prime factors = 2 Pr
15 min read
Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
Given an array of positive numbers, the task is to calculate the absolute difference between sum of non-prime numbers and prime numbers. Note: 1 is neither prime nor non-prime. Examples: Input : 1 3 5 10 15 7 Output : 10 Explanation: Sum of non-primes = 25 Sum of primes = 15 Input : 3 4 6 7 Output : 0 Naive Approach: A simple solution is to travers
15+ min read
Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
Given an array of positive numbers, the task is to calculate the absolute difference between product of non-prime numbers and prime numbers.Note: 1 is neither prime nor non-prime.Examples: Input : arr[] = {1, 3, 5, 10, 15, 7} Output : 45 Explanation : Product of non-primes = 150 Product of primes = 105 Input : arr[] = {3, 4, 6, 7} Output : 3 Naive
15 min read
Absolute difference between the XOR of Non-Prime numbers and Prime numbers of an Array
Given an array arr[] of N positive integers, the task is to calculate the absolute difference between XOR of non-prime numbers and prime numbers. Note that 1 is neither prime nor composite.Examples: Input: arr[] = {1, 3, 5, 10, 15, 7} Output: 4 Xor of non-primes = 10 ^ 15 = 5 Xor of primes = 3 ^ 5 ^ 7 = 1 |5 - 1| = 4Input: arr[] = {3, 4, 6, 7} Outp
8 min read
Count prime numbers up to N that can be represented as a sum of two prime numbers
Given a positive integer N, the task is to find the count of prime numbers less than or equal to N that can be represented as a sum of two prime numbers. Examples: Input: N = 6Output: 1Explanation:5 is the only prime number over the range [1, 6] that can be represented as sum of 2 prime numbers i.e., 5 = 2 + 3, where 2, 3 and 5 are all primes.There
15+ min read
Count prime numbers that can be expressed as sum of consecutive prime numbers
Given an integer N, the task is to find the number of prime numbers up to N that can be expressed as a sum of consecutive primes. Examples: Input: N = 45Output: 3Explanation:Below are the prime numbers up to 45 that can be expressed as sum of consecutive prime numbers: 5 = 2 + 317 = 2 + 3 + 5 + 741 = 2 + 3 + 5 + 7 + 11 + 13Therefore, the count is 3
8 min read
Find all Autobiographical Numbers with given number of digits
Given N as the number of digits, the task is to find all the Autobiographical Numbers whose length is equal to N. An autobiographical number is a number such that the first digit of it counts how many zeroes are there in it, the second digit counts how many ones are there and so on. For example, 1210 has 1 zero, 2 ones, 1 two and 0 threes. Examples
8 min read
Find smallest number with given digits and sum of digits
Given two positive integers P and Q, find the minimum integer containing only digits P and Q such that the sum of the digits of the integer is N. Example: Input: N = 11, P = 4, Q = 7 Output: 47Explanation: There are two possible integers that can be formed from 4 and 7 such that their sum is 11 i.e. 47 and 74. Since we need to find the minimum poss
9 min read
Find the average of k digits from the beginning and l digits from the end of the given number
Given three integers N, K and L. The task is to find the average of the first K digits and the last L digits of the given number N without any digit overlapping.Examples: Input: N = 123456, K = 2, L = 3 Output: 3.0 Sum of first K digits will be 1 + 2 = 3 Sum of last L digits will be 4 + 5 + 6 = 15 Average = (3 + 15) / (2 + 3) = 18 / 5 = 3Input: N =
13 min read
Maximize the given number by replacing a segment of digits with the alternate digits given
Given many N digits. We are also given 10 numbers which represents the alternate number for all the one-digit numbers from 0 to 9. We can replace any digit in N with the given alternate digit to it, but we are only allowed to replace any consecutive segment of numbers for once only, the task is to replace any consecutive segment of numbers such tha
6 min read
Find second smallest number from sum of digits and number of digits
Given the sum of digits as S and the number of digits as D, the task is to find the second smallest number Examples: Input: S = 9, D = 2Output: 27Explanation: 18 is the smallest number possible with sum = 9 and total digits = 2, Whereas the second smallest is 27. Input: S = 16, D = 3Output: 178Explanation: 169 is the smallest number possible with s
8 min read
Number of digits in the nth number made of given four digits
Find the number of digits in the nth number constructed by using 6, 1, 4, and 9 as the only digits in the ascending order. First few numbers constructed by using only 6, 1, 4, and 9 as digits in ascending order would be: 1, 6, 4, 9, 11, 14, 16, 19, 41, 44, 46, 49, 61, 64, 66, 69, 91, 94, 96, 99, 111, 114, 116, 119 and so on. Examples: Input : 6Outp
13 min read
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
Given a number X and an array of N numbers. The task is to print all the numbers in the array whose set of prime factors is a subset of the set of the prime factors of X. Examples: Input: X = 60, a[] = {2, 5, 10, 7, 17} Output: 2 5 10 Set of prime factors of 60: {2, 3, 5} Set of prime factors of 2: {2} Set of prime factors of 5: {5} Set of prime fa
13 min read
Find two numbers A and B of X and Y digits respectively with GCD having Z digits
Given three positive integers X, Y, and Z. The task is to find two numbers A and B of X and Y digits respectively with their GCD having Z digits. where Z ≤ min(X, Y). If there are multiple possible answers print any of them. Examples: Input: X = 2, Y = 3, Z = 1Output: A = 11, B = 100 Explanation: A and B contains 2 and 3 digits respectively and GCD
5 min read
Article Tags :
Practice Tags :