Closest Prime Numbers in Range - Sieve of Eratosthenes [JS]

Description 

Solution: Sieve of Eratosthenes

Use the sieve of eratosthenes to find prime numbers.
How it works:

  • Eliminate the multiples of each prime number.
  • When we reach a number which is marked as prime, we know that it is definitely prime because it hasn't been covered by the smaller prime numbers.

Why do we start j at i * i instead of 2 * i?

  • Because all previous smaller prime factors have already been covered.
  • e.g: i = 7
  • At this point, the prime factors 23, and 5 have been covered, so 2 * 73 * 7, and 5 * 7 have all been marked as non-prime already, therefore we can start directly at i * i.

It can be proven that the minimum difference between two consecutive prime numbers is 2. The only smaller difference is 1 for [2,3].
Therefore, when we find a difference of 1 or 2, we can return immediately.

n = right
Time Complexity: O(n log log n) 367ms
Space Complexity: O(n) 51.1MB

var closestPrimes = function(left, right) {
  let previousPrime = -Infinity, minDiff = Infinity, ans = [-1, -1];
  let isPrime = Array(right + 1).fill(1);
  for (let i = 2; i <= right; i++) {
    if (i >= left && isPrime[i]) {
      let diff = i - previousPrime;
      if (diff <= 2) return [previousPrime, i]; // prime gap of 2
      if (diff < minDiff) {
        minDiff = diff;
        ans = [previousPrime, i];
      }
      previousPrime = i;
    }
    if (isPrime[i]) {
      for (let j = i * i; j <= right; j += i) {
        isPrime[j] = 0;
      }
    }
  }
  return ans;
};

Comments

Popular posts from this blog

Maximum Value of an Ordered Triplet II - Two Solutions [JS]

Maximum Sum of Distinct Subarrays With Length K - Sliding Window w/ Two Pointers & Set [JS]

Sum of Prefix Scores of Strings - Trie [JS]

Maximum Count of Positive Integer and Negative Integer - Binary Search [JS]

Count Subarrays With Median K - Count Left & Right Balance [JS]