给定两个整数L和R ,任务是找到[L,R]范围内任意两个质数之间的最小差。
例子:
Input: L = 21, R = 50
Output: 2
(29, 31) and (41, 43) are the only valid pairs
that give the minimum difference.
Input: L = 1, R = 11
Output: 1
The difference between (2, 3) is minimum.
方法:
- 使用Eratosthenes筛子找出直到R的所有质数。
- 现在从L开始,找到该范围内任意两个质数之间的差,并更新到目前为止的最小差。
- 如果范围内的质数小于2,则打印-1 。
- 否则打印最小差异。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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 return the minimum difference
// between any two prime numbers
// from the given range [L, R]
int minDifference(int L, int R)
{
// Find the first prime from the range
int fst = 0;
for (int i = L; i <= R; i++) {
if (isPrime[i]) {
fst = i;
break;
}
}
// Find the second prime from the range
int snd = 0;
for (int i = fst + 1; i <= R; i++) {
if (isPrime[i]) {
snd = i;
break;
}
}
// If the number of primes in
// the given range is < 2
if (snd == 0)
return -1;
// To store the minimum difference between
// two consecutive primes from the range
int diff = snd - fst;
// Range left to check for primes
int left = snd + 1;
int right = R;
// For every integer in the range
for (int i = left; i <= right; i++) {
// If the current integer is prime
if (isPrime[i]) {
// If the difference between i
// and snd is minimum so far
if (i - snd <= diff) {
fst = snd;
snd = i;
diff = snd - fst;
}
}
}
return diff;
}
// Driver code
int main()
{
// Generate primes
sieve();
int L = 21, R = 50;
cout << minDifference(L, R);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static int sz = (int) 1e5;
static boolean []isPrime = new boolean [sz + 1];
// Function for Sieve of Eratosthenes
static void sieve()
{
Arrays.fill(isPrime, 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 return the minimum difference
// between any two prime numbers
// from the given range [L, R]
static int minDifference(int L, int R)
{
// Find the first prime from the range
int fst = 0;
for (int i = L; i <= R; i++)
{
if (isPrime[i])
{
fst = i;
break;
}
}
// Find the second prime from the range
int snd = 0;
for (int i = fst + 1; i <= R; i++)
{
if (isPrime[i])
{
snd = i;
break;
}
}
// If the number of primes in
// the given range is < 2
if (snd == 0)
return -1;
// To store the minimum difference between
// two consecutive primes from the range
int diff = snd - fst;
// Range left to check for primes
int left = snd + 1;
int right = R;
// For every integer in the range
for (int i = left; i <= right; i++)
{
// If the current integer is prime
if (isPrime[i])
{
// If the difference between i
// and snd is minimum so far
if (i - snd <= diff)
{
fst = snd;
snd = i;
diff = snd - fst;
}
}
}
return diff;
}
// Driver code
public static void main(String []args)
{
// Generate primes
sieve();
int L = 21, R = 50;
System.out.println(minDifference(L, R));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
from math import sqrt
sz = int(1e5);
isPrime = [True] * (sz + 1);
# Function for Sieve of Eratosthenes
def sieve() :
isPrime[0] = isPrime[1] = False;
for i in range(2, int(sqrt(sz)) + 1) :
if (isPrime[i]) :
for j in range(i * i, sz, i) :
isPrime[j] = False;
# Function to return the minimum difference
# between any two prime numbers
# from the given range [L, R]
def minDifference(L, R) :
# Find the first prime from the range
fst = 0;
for i in range(L, R + 1) :
if (isPrime[i]) :
fst = i;
break;
# Find the second prime from the range
snd = 0;
for i in range(fst + 1, R + 1) :
if (isPrime[i]) :
snd = i;
break;
# If the number of primes in
# the given range is < 2
if (snd == 0) :
return -1;
# To store the minimum difference between
# two consecutive primes from the range
diff = snd - fst;
# Range left to check for primes
left = snd + 1;
right = R;
# For every integer in the range
for i in range(left, right + 1) :
# If the current integer is prime
if (isPrime[i]) :
# If the difference between i
# and snd is minimum so far
if (i - snd <= diff) :
fst = snd;
snd = i;
diff = snd - fst;
return diff;
# Driver code
if __name__ == "__main__" :
# Generate primes
sieve();
L = 21; R = 50;
print(minDifference(L, R));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
static int sz = (int) 1e5;
static Boolean []isPrime = new Boolean [sz + 1];
// Function for Sieve of Eratosthenes
static void sieve()
{
for(int i = 2; i< sz + 1; i++)
isPrime[i] = true;
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 return the minimum difference
// between any two prime numbers
// from the given range [L, R]
static int minDifference(int L, int R)
{
// Find the first prime from the range
int fst = 0;
for (int i = L; i <= R; i++)
{
if (isPrime[i])
{
fst = i;
break;
}
}
// Find the second prime from the range
int snd = 0;
for (int i = fst + 1; i <= R; i++)
{
if (isPrime[i])
{
snd = i;
break;
}
}
// If the number of primes in
// the given range is < 2
if (snd == 0)
return -1;
// To store the minimum difference between
// two consecutive primes from the range
int diff = snd - fst;
// Range left to check for primes
int left = snd + 1;
int right = R;
// For every integer in the range
for (int i = left; i <= right; i++)
{
// If the current integer is prime
if (isPrime[i])
{
// If the difference between i
// and snd is minimum so far
if (i - snd <= diff)
{
fst = snd;
snd = i;
diff = snd - fst;
}
}
}
return diff;
}
// Driver code
public static void Main(String []args)
{
// Generate primes
sieve();
int L = 21, R = 50;
Console.WriteLine(minDifference(L, R));
}
}
// This code is contributed by 29AjayKumar
输出:
2