def evaluate_prime(prime): # Evaluates a prime number and adds a key: value pair to prime_table # with a list containing each set of pairs for that prime organized by # number of pairs candidate = Euler.previous_prime(prime) answer = [] # This will be the value we return finished = False pairs = [prime] while finished == False: while candidate > 2: if is_pair(prime, candidate) == True: pairs.append(candidate) candidate -= 2 if len(pairs) > 1: answer.append((len(pairs), pairs)) candidate = Euler.previous_prime(pairs[-1]) pairs = pairs[:-1] if len(pairs) < 2: finished = True if len(answer) == 0: prime_table[prime] = False else: prime_table[prime] = answer
# Problem 70: Totient permutation # candidate: 7026037 import time, Euler, math t1 = time.clock() limit = 10000000 root = Euler.previous_prime(int(math.sqrt(limit))) # 3137 is the largest prime the square of which is < 10,000,000, # Equivalent to Euler.previous_prime(int(math.sqrt(10000000))) primes = {0: root} large_prime_counter = 0 small_prime_counter = 0 composites = {} large = root small = root while small > 2: composite = large*small # Deal with the case where composite is less than limit if composite < limit: phi = composite - (large + small - 1) # Put the composite in the dictionary if it's a permutation if (int("".join(sorted([x for x in str(composite)]))) == int("".join(sorted([x for x in str(phi)])))): composites[composite] = [large, small, phi] # Increase large and add it to primes if necessary