MX = 10 ** 6 + 1 primes = [] is_prime = [True] * MX for i inrange(2, MX): if is_prime[i]: primes.append(i) for j inrange(i * i, MX, i): is_prime[j] = False
classSolution: deffindPrimePairs(self, n: int) -> List[List[int]]: if n % 2: return [[2, n - 2]] if n > 4and is_prime[n - 2] else [] ans = [] for x in primes: y = n - x if y < x: break if is_prime[y]: ans.append([x, y]) return ans
const mx = 1e6 var primes []int var isP = [mx + 1]bool{}
funcinit() { for i := 2; i <= mx; i++ { isP[i] = true } for i := 2; i <= mx; i++ { if isP[i] { primes = append(primes, i) for j := i * i; j <= mx; j += i { isP[j] = false } } } }
funcfindPrimePairs(n int) (ans [][]int) { if n%2 > 0 { if n > 4 && isP[n-2] { return [][]int{ {2, n - 2} } } return } for _, x := range primes { y := n - x if y < x { break } if isP[y] { ans = append(ans, []int{x, y}) } } return }