def problem58(): length = 7 primes = 8 total = 13 while primes/total > 0.1: length += 2 primes += quantify(corners(length**2), pred=is_prime) total += 4 return length
def problem74(): # Known chain loop lengths given in problem description. We will use this # dictionary to cache all further results as we calculate them. known_loops = {145: 1, 169: 3, 1454: 3, 871: 2, 872: 2, 69: 5, 78: 4, 540: 2} lengths = (chain_length(n) for n in range(1, 1000000+1)) def chain_length(n): chain = [n] next = sum_factorial_digits(n) while True: if next in chain: # We have found a new loop, add to the cache. result = known_loops[n] = len(chain) return result if next in known_loops: # We have found a known loop, add its length to current chain. result = known_loops[n] = len(chain) + known_loops[next] return result # We haven't found a loop, continue to investigate the chain. chain.append(next) next = sum_factorial_digits(next) return quantify(lengths, pred=lambda x: x == 60)
def is_smallest_member(num): """Does the number satisfy the problem specification?""" return any(quantify(family(num, indices), pred=is_prime) == 8 for indices in find_indices(num))
def problem55(): return quantify(range(1, 10000), pred=is_lychrel)
def problem57(): generate_tails = iterate(tail, 2) # yields 2, tail(2), tail(tail(2)), ... expansions = (1 + Fraction(1, t) for t in generate_tails) return quantify(take(1000, expansions), pred=check_numerator)
def problem64(): continued_fractions = (continued_fraction_sqrt(i) for i in range(2, 10000+1)) odd_period = lambda x: len(x) % 2 == 0 # The first element is not part of the period. return quantify(continued_fractions, pred=odd_period)