Smallest prime number in given Range
Last Updated :
17 Aug, 2023
Given two integers L and R, denoting a range [L, R], the task is to find the smallest prime number present in the range.
Examples:
Input: L = 6, R = 11
Output: 7
Explanation: In the range of 6 to 11
6 is divisible by 1, 2, 3 and 6.
7 is divisible by 1 and 7, hence this is a prime and the first in the range.
So 7 is smallest prime in the given range.
Input: L = 14, R = 19
Output: 17
Explanation: 14 is divisible by 1, 2, 7 and 14.
15 is divisible by 1, 3, 5 and 15.
16 is divisible by 1, 2, 4, 8 and 16.
17 is divisible by 1 and 17.
So, in the range of 14 to 19, 17 is the smallest prime.
Approach: The problem can be solved based on the following idea:
Traverse from L to R and check if the number is prime or not. The first prime number is the smallest, as we are traversing from smaller end to higher end.
Follow the steps mentioned below to implement the idea:
- Start iteration from i = L to R:
- Check if i is prime or not:
- For this run a loop from j = 2 to the square root of i:
- If i is divisible by any value of j, it is not a prime.
- Otherwise, if i is not divisible by any value of j, i is a prime.
- If i is a prime, break the loop and i the smallest prime.
- Return the value of the smallest prime as the required answer.
Below is the implementation of the above approach:
C++
// C++ code to implement the idea:
#include <bits/stdc++.h>
using namespace std;
// Function to check if the number
// is a prime or not
int isPrime(int n)
{
if (n < 2) {
return 0;
}
else {
for (int i = 2; i < sqrt(n); i++) {
if (n % i == 0)
return 0;
}
}
return 1;
}
// Function to find the smallest
// prime in range
int minPrimeRange(int x, int y)
{
for (int i = x; i <= y; i++) {
if (isPrime(i))
return i;
}
return -1;
}
// Driver code
int main()
{
int L = 14, R = 19;
// Function call
int ans = minPrimeRange(L, R);
cout << ans;
return 0;
}
Java
import java.util.*;
public class Main {
// Function to check if the number is a prime or not
public static boolean isPrime(int n) {
if (n < 2) {
return false;
} else {
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
}
return true;
}
// Function to find the smallest prime in range
public static int minPrimeRange(int x, int y) {
for (int i = x; i <= y; i++) {
if (isPrime(i))
return i;
}
return -1;
}
// Driver code
public static void main(String[] args) {
int L = 14, R = 19;
// Function call
int ans = minPrimeRange(L, R);
System.out.println(ans);
}
}
Python
import math
# Function to check if the number is a prime or not
def isPrime(n):
if n < 2:
return False
else:
for i in range(2, int(math.sqrt(n))+1):
if n % i == 0:
return False
return True
# Function to find the smallest prime in range
def minPrimeRange(x, y):
for i in range(x, y+1):
if isPrime(i):
return i
return -1
# Driver code
if __name__ == "__main__":
L = 14
R = 19
# Function call
ans = minPrimeRange(L, R)
print(ans)
C#
using System;
namespace PrimeRange
{
class Program
{
// Function to check if the number
// is a prime or not
static bool IsPrime(int n)
{
if (n < 2)
{
return false;
}
else
{
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
}
return true;
}
// Function to find the smallest
// prime in range
static int MinPrimeRange(int x, int y)
{
for (int i = x; i <= y; i++)
{
if (IsPrime(i))
return i;
}
return -1;
}
static void Main(string[] args)
{
int L = 14, R = 19;
// Function call
int ans = MinPrimeRange(L, R);
Console.WriteLine(ans);
}
}
}
// This code is contributed by Dwaipayan Bandyopadhyay
JavaScript
// JS code to implement the approach:
function isPrime(n) {
if (n < 2) {
return 0;
} else {
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) {
return 0;
}
}
}
return 1;
}
function minPrimeRange(x, y) {
for (let i = x; i <= y; i++) {
if (isPrime(i)) {
return i;
}
}
return -1;
}
// Driver code
const L = 14;
const R = 19;
// Function call
const ans = minPrimeRange(L, R);
console.log(ans);
Time Complexity: O(N√N)
Auxiliary Space: O(1)
Similar Reads
What is the smallest 3 digit prime number? The smallest 3 digit prime number is 101. Prime Number is a number which is only divisible by 1 and the number itself. It has only 2 factors. Let's find the smallest 3 digit prime number in this article below. Smallest 3 digit Prime NumberThe smallest 3 digit prime number is 101. A prime number has
6 min read
Smallest number by rearranging digits of a given number Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of a given number. Examples: Input: n = 846903Output: 304689Input: n = 55010Output: 10055Input: n = -40505Output: -55400Steps to find the smallest number. Count the frequency of each digit in the number.If i
7 min read
Smallest expression to represent a number using single digit Given a number N and a digit D, we have to form an expression or equation that contains only D and that expression evaluates to N. Allowed operators in an expression are +, -, *, and / . Find the minimum length expression that satisfies the condition above and D can only appear in the expression at
15+ min read
Finding the Smallest Number Among Three in R Writing a function to determine the smallest number among three given numbers in R is a fundamental exercise in understanding how to work with functions, control structures, and basic comparisons in R. This task can be broken down into several key components, each of which will be discussed in detai
3 min read
M-th smallest number having k number of set bits. Given two non-negative integers m and k. The problem is to find the m-th smallest number having k number of set bits.Constraints: 1 <= m, k.Examples: Input : m = 4, k = 2 Output : 9 (9)10 = (1001)2, it is the 4th smallest number having 2 set bits. Input : m = 6, k = 4 Output : 39 Approach: Follow
6 min read
Smallest K digit number divisible by X Integers X and K are given. The task is to find the smallest K-digit number divisible by X. Examples : Input : X = 83, K = 5 Output : 10043 10040 is the smallest 5 digit number that is multiple of 83. Input : X = 5, K = 2 Output : 10Recommended PracticeSmallest K digit number divisible by XTry It! A
4 min read