Skip to content

Latest commit

 

History

History
50 lines (35 loc) · 1.26 KB

_2523. Closest Prime Numbers in Range.md

File metadata and controls

50 lines (35 loc) · 1.26 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : March 07, 2025

Last updated : March 07, 2025


Related Topics : Math, Number Theory

Acceptance Rate : 51.61 %


Solutions

Python

class Solution:
    def closestPrimes(self, left: int, right: int) -> List[int]:
        seive = [False, False] + [True] * (right - 1)

        for factor in range(2, int(right ** 0.5) + 1) :
            if seive[factor] :
                for mult in range(factor ** 2, right + 1, factor) :
                    seive[mult] = False

        primes = [x for x in range(left, right + 1) if seive[x]]
        min_pair = [-1, -1]
        min_pair_dist = inf

        for x, y in zip(primes, primes[1:]) :
            if y - x < min_pair_dist :
                min_pair = [x, y]
                min_pair_dist = y - x

                if min_pair_dist == 2 :
                    break

        return min_pair