Ejemplo n.º 1
0
"""

from math import log10
from bisect import bisect_left

from utils.primes import load_primes


def num_digits(n):
    return int(log10(n)) + 1


def rotate(n):
    return n % 10 * 10**int(log10(n)) + n / 10


primes = load_primes()
primes = primes[bisect_left(primes, 100):bisect_left(primes, 1000000)]
prime_set = set(primes)
count = 13
while len(prime_set) > 0:
    n = prime_set.pop()
    for i in xrange(num_digits(n) - 1):
        n = rotate(n)
        if n not in prime_set:
            break
        prime_set.remove(n)
    else:
        count += num_digits(n)
print count
Ejemplo n.º 2
0
# Author: Deddryk
"""
Solution for problem 10.

This was simple using the utils.primes module written for problem 3.

"""

from utils.primes import load_primes

print sum(load_primes())
Ejemplo n.º 3
0
Solution to problem 37

"""

from bisect import bisect_left
from math import log10

from utils.primes import load_primes

def truncate_left(n):
    return n % 10**(int(log10(n)))

def truncate_right(n):
    return n / 10

primes = load_primes()
primes_set = set(primes)
num_truncatable_primes = 0
truncatable_primes_sum = 0
for prime in primes[bisect_left(primes, 10):]:
    if num_truncatable_primes == 11:
        break
    truncl, truncr = truncate_left(prime), truncate_right(prime)
    while truncl != 0 and truncr != 0:
        if truncl not in primes_set or truncr not in primes_set:
            break
        truncl = truncate_left(truncl)
        truncr = truncate_right(truncr)
    else:
        print prime
        num_truncatable_primes += 1
Ejemplo n.º 4
0
"""
Solution to problem 27

Essentially this solution loops through every possible coefficient
a and b and checks to see how many primes are generated for consecutive
n starting at 0.  Some time is saved by using only primes for b since
for the case n = 0, b must be prime.  It uses the utils.primes module
for prime generation and slices the generated list to all primes < 1000,
then makes a set for fast in checks.

"""

from utils.primes import load_primes

primes = set(load_primes())
small_primes = [x for x in primes if x < 1000]

def count_primes(a, b):
    count = 1
    n = 1
    while n*n + a*n + b in primes:
        count += 1
        n += 1
    return count

most = (40, 1, 41)
for a in xrange(-999, 1000):
    for b in small_primes:
        primes_gen = count_primes(a, b)
        if primes_gen > most[0]: