Queries for the smallest and the largest prime number of given digit
Given Q queries where every query consists of an integer D, the task is to find the smallest and the largest prime number with D digits. If no such prime number exists then print -1.
Examples:
Input: Q[] = {2, 5}
Output:
11 97
10007 99991Input: Q[] = {4, 3, 1}
Output:
1009 9973
101 997
1 7
Approach:
- D digit numbers start from 10(D – 1) and end at 10D – 1.
- Now, the task is to find the smallest and the largest prime number from this range.
- To answer a number of queries for prime numbers, Sieve of Eratosthenes can be used to answer whether a number is prime or not.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define MAX 100000 bool prime[MAX + 1]; void SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i <= MAX; i += p) prime[i] = false; } } } // Function to return the smallest prime // number with d digits int smallestPrime(int d) { int l = pow(10, d - 1); int r = pow(10, d) - 1; for (int i = l; i <= r; i++) { // check if prime if (prime[i]) { return i; } } return -1; } // Function to return the largest prime // number with d digits int largestPrime(int d) { int l = pow(10, d - 1); int r = pow(10, d) - 1; for (int i = r; i >= l; i--) { // check if prime if (prime[i]) { return i; } } return -1; } // Driver code int main() { SieveOfEratosthenes(); int queries[] = { 2, 5 }; int q = sizeof(queries) / sizeof(queries[0]); // Perform queries for (int i = 0; i < q; i++) { cout << smallestPrime(queries[i]) << " " << largestPrime(queries[i]) << endl; } return 0; } |
chevron_right
filter_none
Java
// Java implementation of the approach import java.util.*; class GFG { static int MAX = 100000; static boolean []prime = new boolean[MAX + 1]; static void SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" and // initialize all entries it as true. // A value in prime[i] will finally be false // if i is Not a prime, else true. for (int i = 0; i < MAX + 1; i++) { prime[i] = true; } for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Update all multiples of p greater than or // equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i <= MAX; i += p) prime[i] = false; } } } // Function to return the smallest prime // number with d digits static int smallestPrime(int d) { int l = (int) Math.pow(10, d - 1); int r = (int) Math.pow(10, d) - 1; for (int i = l; i <= r; i++) { // check if prime if (prime[i]) { return i; } } return -1; } // Function to return the largest prime // number with d digits static int largestPrime(int d) { int l = (int) Math.pow(10, d - 1); int r = (int) Math.pow(10, d) - 1; for (int i = r; i >= l; i--) { // check if prime if (prime[i]) { return i; } } return -1; } // Driver code public static void main(String[] args) { SieveOfEratosthenes(); int queries[] = { 2, 5 }; int q = queries.length; // Perform queries for (int i = 0; i < q; i++) { System.out.println(smallestPrime(queries[i]) + " " + largestPrime(queries[i])); } } } // This code is contributed by Rajput-Ji |
chevron_right
filter_none
Python3
# Python3 implementation of the approach from math import sqrt MAX = 100000 # Create a boolean array "prime[0..n]" and # initialize all entries it as true. # A value in prime[i] will finally be false # if i is Not a prime, else true. prime = [True] * (MAX + 1) def SieveOfEratosthenes() : for p in range(2, int(sqrt(MAX)) + 1) : # If prime[p] is not changed, # then it is a prime if (prime[p] == True) : # Update all multiples of p greater than or # equal to the square of it # numbers which are multiple of p and are # less than p^2 are already been marked. for i in range(p * p, MAX + 1, p) : prime[i] = False; # Function to return the smallest prime # number with d digits def smallestPrime(d) : l = 10 ** (d - 1); r = (10 ** d) - 1; for i in range(l, r + 1) : # check if prime if (prime[i]) : return i; return -1; # Function to return the largest prime # number with d digits def largestPrime(d) : l = 10 ** (d - 1); r = (10 ** d) - 1; for i in range(r, l , -1) : # check if prime if (prime[i]) : return i; return -1; # Driver code if __name__ == "__main__" : SieveOfEratosthenes(); queries = [ 2, 5 ]; q = len(queries); # Perform queries for i in range(q) : print(smallestPrime(queries[i]), " ", largestPrime(queries[i])); # This code is contributed by AnkitRai01 |
chevron_right
filter_none
C#
// C# implementation of the approach using System; class GFG { static int MAX = 100000; static bool []prime = new bool[MAX + 1]; static void SieveOfEratosthenes() { // Create a boolean array "prime[0..n]" and // initialize all entries it as true. // A value in prime[i] will finally be false // if i is Not a prime, else true. for (int i = 0; i < MAX + 1; i++) { prime[i] = true; } for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, // then it is a prime if (prime[p] == true) { // Update all multiples of p greater than // or equal to the square of it // numbers which are multiple of p and are // less than p^2 are already been marked. for (int i = p * p; i <= MAX; i += p) prime[i] = false; } } } // Function to return the smallest prime // number with d digits static int smallestPrime(int d) { int l = (int) Math.Pow(10, d - 1); int r = (int) Math.Pow(10, d) - 1; for (int i = l; i <= r; i++) { // check if prime if (prime[i]) { return i; } } return -1; } // Function to return the largest prime // number with d digits static int largestPrime(int d) { int l = (int) Math.Pow(10, d - 1); int r = (int) Math.Pow(10, d) - 1; for (int i = r; i >= l; i--) { // check if prime if (prime[i]) { return i; } } return -1; } // Driver code public static void Main(String[] args) { SieveOfEratosthenes(); int []queries = { 2, 5 }; int q = queries.Length; // Perform queries for (int i = 0; i < q; i++) { Console.WriteLine(smallestPrime(queries[i]) + " " + largestPrime(queries[i])); } } } // This code is contributed by 29AjayKumar |
chevron_right
filter_none
Output:
11 97 10007 99991
Recommended Posts:
- Largest and smallest digit of a number
- Check if the first and last digit of the smallest number forms a prime
- Largest number less than N whose each digit is prime number
- Smallest and Largest sum of two n-digit numbers
- Smallest and Largest N-digit perfect squares
- Smallest and Largest N-digit perfect cubes
- Sum and product of k smallest and k largest prime numbers in the array
- Largest number less than N with digit sum greater than the digit sum of N
- Queries on sum of odd number digit sums of all the factors of a number
- Queries to count the number of unordered co-prime pairs from 1 to N
- Smallest K digit number divisible by X
- Smallest N digit number which is a multiple of 5
- Check if the first and last digit of number N is prime and their sum is less than K
- Largest K digit number divisible by X
- Largest even digit number not greater than N
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.



