""" The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime. There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97. How many circular primes are there below one million? """ from utils import seive_of_eratosthenes from itertools import permutations primes = [str(i) for i in seive_of_eratosthenes(1000000) if not str(i).count("2") and not str(i).count("4") and not str(i).count("5") and not str(i).count("6") and not str(i).count("8") and not str(i).count("0")] print "Found", len(primes), "primes" count = 2 # 2 and 5 are circular, but filtered out above for i, prime in enumerate(primes): if i % 100 == 0: print "Checking prime", i rotate = prime #rotate the string for j in range(len(prime)): if rotate not in primes: break rotate = rotate[1:] + rotate[0] #print "Rotated to", rotate else: print prime, "is good" count += 1 print count
""" The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Find the sum of the only eleven primes that are both truncatable from left to right and right to left. NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes. """ from utils import seive_of_eratosthenes primes = [] answers = [] for prime in seive_of_eratosthenes(1000000): temp = str(prime) if "0" not in temp and "2" not in temp[1:-1] and "4" not in temp and "6" not in temp and "8" not in temp: primes.append(temp) def is_truncatable(s, left=True): while(len(s)): if s not in primes: return False s = s[1:] if left else s[:-1] else: return True for prime in primes: if is_truncatable(prime, True) and is_truncatable(prime, False): answers.append(prime)
Using computers, the incredible formula n^2 - 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, 79 and 1601, is 126479. Considering quadratics of the form: n^2 + an + b, where |a| < 1000 and |b| < 1000 where |n| is the modulus/absolute value of n e.g. |11| = 11 and |4| = 4 Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0. """ from utils import seive_of_eratosthenes primes = seive_of_eratosthenes(1000000) print "Generated primes" maxn, maxa, maxb = 0, 0, 0 for a in xrange(1, 1000, 2): for b in xrange(1, 1000, 2): f = lambda n, a, b: (n ** 2) + (a * n) + b for n in range(250): if f(n, a, b) not in primes: #print "Finished", a, b, n break if n > maxn: maxn = n maxa = a
""" The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. """ from utils import seive_of_eratosthenes print sum(seive_of_eratosthenes(2000000))
""" By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th) prime is 13. What is the 10001st prime number? """ from utils import seive_of_eratosthenes print seive_of_eratosthenes(110000)[10000]