Skip to content

Instantly share code, notes, and snippets.

@mythmon
Created February 7, 2015 22:36
Show Gist options
  • Save mythmon/b7f5fcdc33fb3621c407 to your computer and use it in GitHub Desktop.
Save mythmon/b7f5fcdc33fb3621c407 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import pytest
from itertools import islice, count, takewhile, tee
from math import sqrt
def divisable(a, b):
return a % b == 0
def _prime_gen():
yield 2
primes = set()
for n in count(3, 2):
for p in primes:
if divisable(n, p):
break
else:
yield n
primes.add(n)
_prime_gen_singleton = _prime_gen()
def prime_gen():
global _prime_gen_singleton
_prime_gen_singleton, ret = tee(_prime_gen_singleton, 2)
return ret
def is_twice_a_square(n):
if n % 2 == 1:
return False
s = n // 2
r = int(sqrt(s))
return r ** 2 == s
def smallest_odd_non_goldbach():
for n in count(3, 2):
for p in takewhile(lambda x: x <= n, prime_gen()):
if is_twice_a_square(n - p):
break
else:
return n
def test_prime_gen():
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23]
assert list(islice(prime_gen(), len(primes))) == primes
@pytest.mark.parametrize("input,expected", [
(0, True),
(1, False),
(2, True),
(3, False),
(4, False),
(8, True),
(12, False),
(18, True),
])
def test_is_twice_a_square(input, expected):
assert is_twice_a_square(input) == expected
def test_smallest_odd_non_goldbach():
assert smallest_odd_non_goldbach() == 5777
if __name__ == '__main__':
print(smallest_odd_non_goldbach())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment