-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.py
More file actions
50 lines (43 loc) · 1.22 KB
/
Copy pathsolution.py
File metadata and controls
50 lines (43 loc) · 1.22 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
import json
import time
import math
from typing import List
def primes(up_to: int) -> List[int]:
candidates = list(range(up_to))
limit = int(up_to**0.5)+1
for i in candidates:
if i in [0,1]:
continue
if i == limit:
break
for j in range(i*2, up_to, i):
candidates[j] = 0
candidates = set(candidates)
candidates.remove(0)
candidates.remove(1)
return sorted(candidates)
_PRIMES = primes(2_000_000)
def is_prime(n: int) -> bool:
limit = int(n**0.5)+1
for prime in _PRIMES:
if prime >= limit:
return True
if n%prime == 0:
return False
def goldbach_valid(n):
for i in range(int((n/2)**0.5)+1):
if is_prime(n - 2*i**2):
return True
return False
def solution() -> int:
for i in range(1, 2_000_000_000, 2):
if is_prime(i):
# Don't check primes
continue
if not goldbach_valid(i):
return i
cpu_s, wall_s = time.process_time(), time.time()
result = solution()
cpu_e, wall_e = time.process_time(), time.time()
cpu_time, wall_time = cpu_e - cpu_s, wall_e - wall_s
print(json.dumps({"solution": result, "cpu": cpu_time, "wall": wall_time}))