def problem_49(): """ Attempt to solve the problem... """ # create a list of primes that are exactly 4 digits primes = [x for x in list(seive_generator(9999) )if int(log10(x)) + 1 == 4] know_seq = [1487, 4817, 8147] for x in primes: perms = set() for v in list(permutations(str(x))): prime = ''.join(v) if len(prime) is not 4: continue prime = int(prime) if prime in primes: perms.add(prime) maches = set() for y in perms: for z in perms: if fabs(y - z) == 3330: maches.add(y) if len(maches) == 3: print x print maches return 0
def problem(): """ Attempt to solve the problem... """ print 'problem #24' g = permutations([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) p = [g.next() for x in xrange(10**6)] digits = ''.join(str(x) for x in p[-1]) print 'the digits of the millionth perm are: %s' % digits
def isPandigital(n): number = "" for i in range(1, n+1): number += str(i) perms = permutations(number) primes = [] for perm in perms: if isprime(int(perm)): primes.append(int(perm)) return primes
def problem_43(): """ Attempt to solve the problem... """ pandigital_sum = 0 for item in map(''.join, permutations('1406357289')): if int(item[7:]) % 17 is not 0: continue if int(item[6:-1]) % 13 is not 0: continue if int(item[5:-2]) % 11 is not 0: continue if int(item[4:-3]) % 7 is not 0: continue if int(item[3:-4]) % 5 is not 0: continue if int(item[2:-5]) % 3 is not 0: continue if int(item[1:-6]) % 2 is not 0: continue pandigital_sum += int(item) return pandigital_sum
def find_magic_ring(ring_size, external_nodes, internal_nodes): """Return a set of magic rings, each of which uses each external node once, and internal node once, and have the same sum. :param ring_size: Number of inner nodes on the ring (same as the number of outer nodes) :param external_nodes: List of numbers to be used as external nodes. Each number on this list will be used once for each magic ring. :param internal_nodes: List of numbers to be used as internal nodes. Each number on this list will be used once for each magic ring. :return: List of magic rings having the specified external and internal nodes in the form: [[(external1, internal1, internal2), (external2, internal2, internal3), ... ring_size number of tuples], [next magic ring]...] """ # Calculate the only possible magic sum for a given set of external/internal node values. expected_magic_sum = (sum(external_nodes) + 2*sum(internal_nodes)) // ring_size # Generate sets of nodes with the same sum magic_sum_sets = [] for e_node in external_nodes: for i_node1 in internal_nodes: i_node2 = expected_magic_sum - e_node - i_node1 if i_node2 in internal_nodes and i_node2 != i_node1: magic_sum_sets.append((e_node, i_node1, i_node2)) # Generate rings magic_rings = [] if len(magic_sum_sets) >= ring_size: external_node_permutations_count = common.permutations(ring_size-1, ring_size-1) for permutation_index in range(external_node_permutations_count): external_node_list =[external_nodes[0]] external_node_list.extend(common.nth_permutation(permutation_index, external_nodes[1:])) magic_rings.extend(next_magic_node([], external_node_list, magic_sum_sets)) # Delete sets that do not cycle back from last node to first node for magic_ring in reversed(magic_rings): if magic_ring[0][1] != magic_ring[-1][2]: magic_rings.remove(magic_ring) return magic_rings
#! /usr/bin/env python from common import isprime, permutations primes = [] for n in xrange(1000, 10000): if isprime(n): primes.append(n) primelist = {} for prime in primes: string_perms = set(permutations(str(prime))) perms = [] for perm in string_perms: p = int(perm) if p > prime and isprime(p): perms.append(p) if len(perms) > 1: perms.sort() for perm_prime in perms: next_prime = perm_prime + perm_prime - prime if next_prime in perms: print prime, perm_prime, next_prime, "- all terms concatenated:", str(prime) + str(perm_prime) + str(next_prime)
#! /usr/bin/env python from common import permutations digits = '123456789' perms = permutations(digits) pandigital_products = [] for perm in perms: for x in range(1, 6): for eq in range(x+1, 7): multiplicand = int(''.join(perm[0:x])) multiplier = int(''.join(perm[x:eq])) product = int(''.join(perm[eq:])) if multiplicand * multiplier == product: print perm, multiplicand, "x", multiplier, "=", product pandigital_products.append(product) print sum(set(pandigital_products))
#! /usr/bin/env python from common import permutations def check_all_multiples(multiples, perms): for m in multiples: if m not in perms: return False return True for n in xrange(1, 1000000): multiples = [] for i in [2, 3, 4, 5, 6]: multiples.append(str(i * n)) perms = permutations(str(n)) if check_all_multiples(multiples, perms): print n, multiples
#! /usr/bin/env python from common import permutations, isprime n = '0123456789' perms = permutations(n) primes = [] for p in range(1, 18): if isprime(p): primes.append(p) def subStringDivisibility(n): for d in range(1, 8): if int(n[d:d+3]) % primes[d-1] != 0: return False return True count = 0 sum = 0 for perm in perms: if subStringDivisibility(perm): count += 1 sum += int(perm) print "the sum of all 0 to 9 pandigital numbers with this property", sum print "count", count