📌  相关文章
📜  查询给定范围内素数之间的最大差异

📅  最后修改于: 2021-05-07 08:35:12             🧑  作者: Mango

给定n个形式为[L,R]的查询。任务是在每个查询的范围内找到两个质数之间的最大差值。如果范围内没有素数,则打印0。所有范围都低于100005。
例子:

Input : Q = 3
        query1 = [2, 5]
        query2 = [2, 2]
        query3 = [24, 28]
Output : 3
         0
         0
In first query, 2 and 5 are prime number 
in the range with maximum difference which 
is 3. In second and third queries, there 
is only 1 prime number in range, so output
is 0.

这个想法是使用Eratosthenes筛和一些预计算来计算素数。
以下是解决问题的步骤:
第1步:使用Eratosthenes算法的筛子查找质数。
步骤2:制作一个数组,假设prefix [] ,其中prefix [i]表示小于或等于i的最大素数。
步骤3:制作一个数组,假设suffix [] ,其中suffix [i]表示大于或等于i的最小素数。
步骤4:现在,对于每个具有[L,R]的查询,请执行以下操作:

if (prefix[R]  R)
    return 0;
  else
    return prefix[R] - suffix[L];

以下是此方法的实现:

C++
// CPP program to find maximum differences between
// two prime numbers in given ranges
#include 
using namespace std;
#define MAX 100005
 
// Declare global variables to assign heap memory and avoid
// stack overflow
bool prime[MAX];
int prefix[MAX], suffix[MAX];
 
// Precompute Sieve, Prefix array, Suffix array
void precompute(int prefix[], int suffix[])
{
    memset(prime, true, sizeof(prime));
 
    // Sieve of Eratosthenes
    for (int i = 2; i * i < MAX; i++) {
        if (prime[i]) {
            for (int j = i * i; j < MAX; j += i)
                prime[j] = false;
        }
    }
 
    prefix[1] = 1;
    suffix[MAX - 1] = 1e9 + 7;
 
    // Precomputing Prefix array.
    for (int i = 2; i < MAX; i++) {
        if (prime[i])
            prefix[i] = i;
        else
            prefix[i] = prefix[i - 1];
    }
 
    // Precompute Suffix array.
    for (int i = MAX - 1; i > 1; i--) {
        if (prime[i])
            suffix[i] = i;
        else
            suffix[i] = suffix[i + 1];
    }
}
 
// Function to solve each query
int query(int prefix[], int suffix[], int L, int R)
{
    if (prefix[R] < L || suffix[L] > R)
        return 0;
    else
        return prefix[R] - suffix[L];
}
 
// Driven Program
int main()
{
    int q = 3;
    int L[] = { 2, 2, 24 };
    int R[] = { 5, 2, 28 };
 
    precompute(prefix, suffix);
 
    for (int i = 0; i < q; i++)
        cout << query(prefix, suffix, L[i], R[i]) << endl;
 
    return 0;
}


Java
// Java program to find maximum differences between
// two prime numbers in given ranges
 
public class GFG {
 
    final static int MAX = 100005;
 
    // Precompute Sieve, Prefix array, Suffix array
    static void precompute(int prefix[], int suffix[])
    {
        boolean prime[] = new boolean[MAX];
        for (int i = 0; i < MAX; i++) {
            prime[i] = true;
        }
 
        // Sieve of Eratosthenes
        for (int i = 2; i * i < MAX; i++) {
            if (prime[i]) {
                for (int j = i * i; j < MAX; j += i) {
                    prime[j] = false;
                }
            }
        }
 
        prefix[1] = 1;
        suffix[MAX - 1] = (int)1e9 + 7;
 
        // Precomputing Prefix array.
        for (int i = 2; i < MAX; i++) {
            if (prime[i]) {
                prefix[i] = i;
            }
            else {
                prefix[i] = prefix[i - 1];
            }
        }
 
        // Precompute Suffix array.
        for (int i = MAX - 2; i > 1; i--) {
            if (prime[i]) {
                suffix[i] = i;
            }
            else {
                suffix[i] = suffix[i + 1];
            }
        }
    }
 
    // Function to solve each query
    static int query(int prefix[], int suffix[], int L,
                     int R)
    {
        if (prefix[R] < L || suffix[L] > R) {
            return 0;
        }
        else {
            return prefix[R] - suffix[L];
        }
    }
 
    // Driven Program
    public static void main(String[] args)
    {
        int q = 3;
        int L[] = { 2, 2, 24 };
        int R[] = { 5, 2, 28 };
 
        int prefix[] = new int[MAX], suffix[]
                                     = new int[MAX];
        precompute(prefix, suffix);
 
        for (int i = 0; i < q; i++) {
            System.out.println(
                query(prefix, suffix, L[i], R[i]));
        }
    }
}
/*This code is contributed by Rajput-Ji*/


Python3
# Python 3 program to find maximum
# differences between two prime numbers
# in given ranges
from math import sqrt
 
MAX = 100005
 
# Precompute Sieve, Prefix array, Suffix array
def precompute(prefix, suffix):
    prime = [True for i in range(MAX)]
 
    # Sieve of Eratosthenes
    k = int(sqrt(MAX))
    for i in range(2, k, 1):
        if (prime[i]):
            for j in range(i * i, MAX, i):
                prime[j] = False
 
    prefix[1] = 1
    suffix[MAX - 1] = int(1e9 + 7)
 
    # Precomputing Prefix array.
    for i in range(2, MAX, 1):
        if (prime[i]):
            prefix[i] = i
        else:
            prefix[i] = prefix[i - 1]
 
    # Precompute Suffix array.
    i = MAX - 2
    while(i > 1):
        if (prime[i]):
            suffix[i] = i
        else:
            suffix[i] = suffix[i + 1]
        i -= 1
 
# Function to solve each query
 
 
def query(prefix, suffix, L, R):
    if (prefix[R] < L or suffix[L] > R):
        return 0
    else:
        return prefix[R] - suffix[L]
 
 
# Driver Code
if __name__ == '__main__':
    q = 3
    L = [2, 2, 24]
    R = [5, 2, 28]
 
    prefix = [0 for i in range(MAX)]
    suffix = [0 for i in range(MAX)]
    precompute(prefix, suffix)
 
    for i in range(0, q, 1):
        print(query(prefix, suffix,
                    L[i], R[i]))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find maximum differences between
// two prime numbers in given ranges
using System;
 
public class GFG {
 
    static readonly int MAX = 100005;
 
    // Precompute Sieve, Prefix array, Suffix array
    static void precompute(int[] prefix, int[] suffix)
    {
        bool[] prime = new bool[MAX];
        for (int i = 0; i < MAX; i++) {
            prime[i] = true;
        }
 
        // Sieve of Eratosthenes
        for (int i = 2; i * i < MAX; i++) {
            if (prime[i]) {
                for (int j = i * i; j < MAX; j += i) {
                    prime[j] = false;
                }
            }
        }
 
        prefix[1] = 1;
        suffix[MAX - 1] = (int)1e9 + 7;
 
        // Precomputing Prefix array.
        for (int i = 2; i < MAX; i++) {
            if (prime[i]) {
                prefix[i] = i;
            }
            else {
                prefix[i] = prefix[i - 1];
            }
        }
 
        // Precompute Suffix array.
        for (int i = MAX - 2; i > 1; i--) {
            if (prime[i]) {
                suffix[i] = i;
            }
            else {
                suffix[i] = suffix[i + 1];
            }
        }
    }
 
    // Function to solve each query
    static int query(int[] prefix, int[] suffix, int L,
                     int R)
    {
        if (prefix[R] < L || suffix[L] > R) {
            return 0;
        }
        else {
            return prefix[R] - suffix[L];
        }
    }
 
    // Driven Program
    public static void Main()
    {
        int q = 3;
        int[] L = { 2, 2, 24 };
        int[] R = { 5, 2, 28 };
 
        int[] prefix = new int[MAX];
        int[] suffix = new int[MAX];
        precompute(prefix, suffix);
 
        for (int i = 0; i < q; i++) {
            Console.WriteLine(
                query(prefix, suffix, L[i], R[i]));
        }
    }
}
 
/*This code is contributed by 29AjayKumar*/


PHP
 1; $i--)
    {
        if ($prime[$i])
            $suffix[$i] = $i;
        else
            $suffix[$i] = $suffix[$i + 1];
    }
}
 
// Function to solve each query
function query($prefix, $suffix, $L, $R)
{
    if ($prefix[$R] < $L || $suffix[$L] > $R)
        return 0;
    else
        return $prefix[$R] - $suffix[$L];
}
 
// Driver Code
$q = 3;
$L = array( 2, 2, 24 );
$R = array( 5, 2, 28 );
 
$prefix = array_fill(0, $MAX + 1, 0);
$suffix = array_fill(0, $MAX + 1, 0);
precompute($prefix, $suffix);
 
for ($i = 0; $i < $q; $i++)
    echo query($prefix, $suffix,
               $L[$i], $R[$i]) . "\n";
 
// This code is contributed by mits
?>


输出:

3
0
0