-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101.py
37 lines (35 loc) · 1.04 KB
/
101.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
36
37
# This problem was asked by Alibaba.
# Given an even number (greater than 2), return two prime numbers whose sum will be equal to the given number.
# A solution will always exist. See https://en.wikipedia.org/wiki/Goldbach%27s_conjecture.
# Example:
# Input: 4
# Output: 2 + 2 = 4
# If there are more than one solution possible, return the lexicographically smaller solution.
# If [a, b] is one solution with a <= b, and [c, d] is another solution with c <= d, then
# [a, b] < [c, d]
# If a < c OR a==c AND b < d.
####
def primes(x):
sieve = [True]*x
sieve[0] = sieve[1] = False
i = 2
while i*i < x:
if sieve[i]:
for cur in range(i*i, x, i):
sieve[cur] = False
i += 1
return [i for i, x in enumerate(sieve) if x]
def goldbach_pair(x):
p = primes(x)
beg = 0
end = len(p)-1
while beg < end:
cur = p[beg] + p[end]
if cur > x:
end -= 1
elif cur < x:
beg += 1
else:
return p[beg], p[end]
####
print(goldbach_pair(10))