-
Notifications
You must be signed in to change notification settings - Fork 0
/
46.py
53 lines (44 loc) · 1.12 KB
/
46.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
What is the smallest odd composite that cannot be written as the sum of a prime
and twice a square?
"""
from lib import euler
from sys import exit
"""
What we're going to do here is iterate over odd integers indefinitely,
filling up an array of primes and squares up to the current integer as we go.
Then we're going to iterate over the primes, subtracting the prime from the
integer, and seeing if the difference can be made up in the list of squares
HURRAH FOR GENERATORS
"""
def squares():
for x in euler.integers(1):
yield x**2
prime_generator = euler.primes()
square_generator = squares()
"""Using dicts for speedy lookup"""
primes = {}
squares = {}
for i in euler.integers(9, 2):
while 1:
p = prime_generator.next()
primes[p] = True
if p > i: break
while 1:
s = square_generator.next()
squares[s] = True
if s > i: break
if i in primes: continue
found = False
for p in primes.keys():
diff = i - p
if diff > 0:
diff/=2
if int(diff) == diff:
diff = int(diff)
if diff in squares:
found = True
break
if not found:
print i
break