-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler46.py
36 lines (32 loc) · 1.12 KB
/
euler46.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
""" What is the smallest odd composite that cannot be written as the sum of a prime
and twice a square? """
from math import floor
def primes_up_to(n):
""" Erastothenes """
primes = [True]*n
primes[0] = False
for i in range(n):
if not primes[i]:
continue
next = 2 * (i + 1)
while next <= n:
primes[next - 1] = False
next += (i + 1)
return [x + 1 for x in range(n) if primes[x]]
def meets_criterion(n, long, short):
""" true if there are elements a in long and b in short s.th a + b = n """
for b in short:
if n - b in long:
return True
return False
def main(upper_limit):
""" Make lists of primes and squares then chomp through to find a counter-example"""
primes = primes_up_to(upper_limit)
twice_squares = [2 * x ** 2 for x in range(1, floor(upper_limit ** 0.5))]
for odd in [x for x in range(3, upper_limit + 1, 2) if x not in primes]:
if not meets_criterion(odd, primes, twice_squares):
return odd
return -1
if __name__ == '__main__':
upper_limit = 6000
print(main(upper_limit))