예제 #1
0
    if N < 2:
        return False
    if N == 2:
        return True
    if N % 2 == 0:
        return False
    if N < 1000:
        return (primes[N] != 0)
    i = 3
    while i**2 <= N:
        if N % i == 0:
            return False
        i += 2
    return True

primes = raw_sieve(1000)

A = False
B = False
MAX_N = 0

# When n == 0, n^2 + a*n + b = b, so b must be prime
for b in reversed(primes):
    if b < MAX_N:
        continue
    for a in xrange(-999, 1000):
        n = 0
        while is_prime(n**2 + a*n + b, primes):
            n += 1
        if n > MAX_N:
            MAX_N = n
예제 #2
0
from sieve import raw_sieve

nums = raw_sieve(1000000)

# Loop through. If any rotation isn't prime, 0 it out.
def rotated(n):
    yield n
    N = str(n)
    for i in range(1, len(N)):
        yield int(N[i:] + N[:i])

for n in nums:
    if n != 0:
        if any([nums[p] == 0 for p in rotated(n)]):
            nums[n] = 0

# Filter and count
print len([x for x in nums if x != 0])