Created
April 3, 2012 09:16
-
-
Save shieldsd/2290607 to your computer and use it in GitHub Desktop.
Project Euler #35
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import takewhile | |
from itertools import count | |
from math import sqrt | |
from math import log | |
def rotate(n): | |
d = int(log(n, 10)) | |
m = 10 ** d | |
for _ in range(d): | |
r = n % 10 | |
n /= 10 | |
n += m * r | |
yield n | |
def isprime(n): | |
for x in range(2, int(sqrt(n)) + 1): | |
if n % x == 0: | |
return 0 | |
return 1 | |
def isprimel(l): | |
for p in l: | |
if not isprime(p): | |
return 0 | |
return 1 | |
def prime(): | |
primes = [] | |
for n in count(2): | |
composite = 0 | |
for p in primes: | |
if not n % p: | |
composite = 1 | |
break | |
elif p ** 2 > n: | |
break | |
if not composite: | |
primes.append(n) | |
yield n | |
print sum(1 for _ in | |
(p for p in | |
(takewhile(lambda n: n < 1000000, prime())) | |
if isprimel(rotate(p)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment