📜  求最近素数的Java程序

📅  最后修改于: 2022-05-13 01:55:11.835000             🧑  作者: Mango

求最近素数的Java程序

给定一个数 N,你必须打印它最接近的素数。质数可以小于、等于或大于给定的数字。

条件: 1≤N≤100000

例子:

Input : 16
Output: 17

Explanation: The two nearer prime number of 16 are 13 and 17. But among these, 
17 is the closest(As its distance is only 1(17-16) from the given number).

Input : 97
Output : 97

Explanation : The closest prime number in this case is the given number number 
itself as the distance is 0 (97-97).

方法 :

  1. 使用埃拉托色尼筛法将所有素数存储在一个向量中。
  2. 将 vector 中的所有元素复制到新数组中。
  3. 使用上限查找数组中给定数字的上限。
  4. 由于数组本质上已经排序,因此比较数组中先前和当前的索引数字。
  5. 返回具有最小差异的数字。

下面是该方法的实现。

Java
// Closest Prime Number in Java
  
import java.util.*;
import java.lang.*;
  
public class GFG {
  
    static int max = 100005;
  
    static Vector primeNumber = new Vector<>();
  
    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.
        boolean prime[] = new boolean[max + 1];
        for (int i = 0; i <= max; 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
                for (int i = p * p; i <= max; i += p)
                    prime[i] = false;
            }
        }
  
        // Print all prime numbers
        for (int i = 2; i <= max; i++) {
  
            if (prime[i] == true)
                primeNumber.add(i);
        }
    }
  
    static int upper_bound(Integer arr[], int low, int high,
                           int X)
    {
        // Base Case
        if (low > high)
            return low;
  
        // Find the middle index
        int mid = low + (high - low) / 2;
  
        // If arr[mid] is less than
        // or equal to X search in
        // right subarray
        if (arr[mid] <= X) {
            return upper_bound(arr, mid + 1, high, X);
        }
  
        // If arr[mid] is greater than X
        // then search in left subarray
        return upper_bound(arr, low, mid - 1, X);
    }
  
    public static int closetPrime(int number)
    {
  
        // We will handle it (for number = 1) explicitly
        // as the lower/left number of 1 can give us
        // negative index which will cost Runtime Error.
        if (number == 1)
            return 2;
        else {
  
            // calling seive of eratosthenes to
            // fill the array into prime numbers
            sieveOfEratosthenes();
  
            Integer[] arr = primeNumber.toArray(
                new Integer[primeNumber.size()]);
            // searching the index
            int index
                = upper_bound(arr, 0, arr.length, number);
  
            if (arr[index] == number
                || arr[index - 1] == number)
                return number;
            else if (Math.abs(arr[index] - number)
                     < Math.abs(arr[index - 1] - number))
                return arr[index];
            else
                return arr[index - 1];
        }
    }
    // Driver Program
    public static void main(String[] args)
    {
        int number = 100;
        System.out.println(closetPrime(number));
    }
}


输出
101






时间复杂度: O(N log(log(N)))

空间复杂度: O(N)