def problem49(): """ The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence. What 12-digit number do you form by concatenating the three terms in this sequence? """ start_n = 1491 end_n = 9999 prime_map = mlib.prime_sieve(10**5, output={}) for n in range(start_n, end_n, 2): for step in range(2, (end_n-n)/2, 2): if (n in prime_map and n+step in prime_map and n+step*2 in prime_map): n1 = list(str(n)) n2 = list(str(n+step)) n3 = list(str(n+2*step)) n1.sort() n2.sort() n3.sort() if n1 == n2 and n2 == n3: return str(n)+str(n+step)+str(n+2*step)
def problem12(): """ The sequence of triangle numbers is generated by adding the natural numbers. So the 7^(th) triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... Let us list the factors of the first seven triangle numbers: We can see that 28 is the first triangle number to have over five divisors. What is the value of the first triangle number to have over five hundred divisors? """ def gen_triangle_num(): i = 1 num = 0 while True: num += i i += 1 yield num primelist = mlib.prime_sieve(10**6, output=[]) generator = gen_triangle_num() while True: num = generator.next() if len(mlib.get_factors(num, primelist)) > 500: return num
def problem21(): """ Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a # b, then a and b are an amicable pair and each of a and b are called amicable numbers. For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220. Evaluate the sum of all the amicable numbers under 10000. """ primelist = mlib.prime_sieve(10 ** 4, output=[]) amicable_map = {} am_sum = 0 for i in range(1, 10 ** 4 + 1): factors = mlib.get_factors(i, primelist) amicable_map[i] = sum(factors[:-1]) for i in amicable_map: if amicable_map[i] != i and amicable_map[i] in amicable_map and amicable_map[amicable_map[i]] == i: am_sum += i return am_sum
def problem58(): prime_map = mlib.prime_sieve(2*10**6) side_len = 3 num_prime = 0.0 num = 0 while side_len < 20000: c = side_len**2 corners = [n for n in range(c, c - side_len*3, -side_len+1)] num += len(corners) for p in corners: if p in prime_map: num_prime += 1 # print num_prime, num if num_prime / num < 0.1: return side_len side_len += 2
def problem23(): """ A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number. A number whose proper divisors are less than the number is called deficient and a number whose proper divisors exceed the number is called abundant. As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit. Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers. """ primelist = mlib.prime_sieve(10 ** 5, output=[]) abundant = [] for i in range(1, 28123 + 1): factors = mlib.get_factors(i, primelist) if sum(factors[:-1]) > i: abundant.append(i) ab_sum = {} ret_sum = 0 for i in range(0, len(abundant)): if abundant[i] > 28123: break for j in range(0, len(abundant)): ab_sum[abundant[i] + abundant[j]] = 1 if abundant[i] + abundant[j] > 28123: break for i in range(1, 28123 + 1): if i not in ab_sum: ret_sum += i return ret_sum
def problem329(): """ Susan has a prime frog. Her frog is jumping around over 500 squares numbered 1 to 500. He can only jump one square to the left or to the right, with equal probability, and he cannot jump outside the range [1;500]. (if it lands at either end, it automatically jumps to the only available square on the next move.) When he is on a square with a prime number on it, he croaks 'P' (PRIME) with probability 2/3 or 'N' (NOT PRIME) with probability 1/3 just before jumping to the next square. When he is on a square with a number on it that is not a prime he croaks 'P' with probability 1/3 or 'N' with probability 2/3 just before jumping to the next square. Given that the frog's starting position is random with the same probability for every square, and given that she listens to his first 15 croaks, what is the probability that she hears the sequence PPPPNNPPPNPPNPN? Give your answer as a fraction p/q in reduced form. """ seq = "PPPPNNPPPNPPNPN" primes = mathlib.prime_sieve(500) @memoized def f(k, seq): curr, rest_seq = seq[0], seq[1:] if k in primes: p = Fraction(2,3) if curr == 'P' else Fraction(1,3) else: p = Fraction(2,3) if curr == 'N' else Fraction(1,3) if len(rest_seq) == 0: return p elif k == 1: return p * f(2, rest_seq) elif k == 500: return p * f(499, rest_seq) else: return p * (f(k-1, rest_seq) + f(k+1, rest_seq)) / 2 return sum(f(i, seq) for i in range(1,501)) / 500
def problem27(): """ Euler published the remarkable quadratic formula: n^2 + n + 41 It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 40^(2) + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41^2 + 41 + 41 is clearly divisible by 41. 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 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. """ prime_map = mlib.prime_sieve(1000 ** 2, output={}) max_chain = (0, 0, 0) # n**2 + a*n + b for a in range(-1000, 1000): for b in range(-1000, 1000): n = 0 chain = 0 while True: p = (n + a) * n + b if p in prime_map: chain += 1 n += 1 else: break if chain > max_chain[0]: max_chain = (chain, a, b) # print max_chain return max_chain[1] * max_chain[2]
def problem58(): """ Starting with 1 and spiralling anticlockwise in the following way, a square spiral with side length 7 is formed. If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%? """ prime_map = mlib.prime_sieve(2*10**6) side_len = 3 num_prime = 0.0 num = 0 while side_len < 20000: c = side_len**2 corners = [n for n in range(c, c - side_len*3, -side_len+1)] num += len(corners) for p in corners: if p in prime_map: num_prime += 1 if num_prime / num < 0.1: return side_len side_len += 2
def problem10(): """ The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. """ return sum(mlib.prime_sieve(2*10**6))