-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsol.py
35 lines (31 loc) · 920 Bytes
/
sol.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from typing import List
import math
class Solution:
def closestPrimes(self, left: int, right: int) -> List[int]:
prev = math.inf
diff = math.inf
pair = [-1, -1]
for cur in range(left, right + 1):
if not self.__is_prime(cur):
continue
cur_diff = abs(cur - prev)
if cur_diff < diff:
diff = cur_diff
pair = [prev, cur]
if diff <= 2:
return pair
prev = cur
return pair
def __is_prime(self, num: int) -> bool:
if num < 2:
return False
if num <= 3:
return True
if num % 2 == 0:
return False
if num % 3 == 0:
return False
for i in range(5, int(math.sqrt(num)) + 1, 2):
if num % i == 0:
return False
return True