def is_special_number(numbers): i = 1 for d in divisibleBy: if permutations.combine_numbers(numbers[i : (i + 3)]) % d != 0: return False i += 1 return True
import primes import permutations primesToCheck = primes.create_primes(sqrt(987654321)) primeFound = False def is_prime(number): limit = sqrt(number) for p in primesToCheck: if p > limit: return True elif number % p == 0: return False return True for x in xrange(10, 1, -1): allPermutations = permutations.permutations(range(1, x)) for perm in sorted([permutations.combine_numbers(y) for y in allPermutations], reverse=True): if is_prime(perm): print str(perm) primeFound = True break if primeFound: break
192 x 1 = 192 192 x 2 = 384 192 x 3 = 576 By concatenating each product we get the 1 to 9 pandigital, 192384576. We will call 192384576 the concatenated product of 192 and (1,2,3) The same can be achieved by starting with 9 and multiplying by 1, 2, 3, 4, and 5, giving the pandigital, 918273645, which is the concatenated product of 9 and (1,2,3,4,5). What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n > 1? """ import permutations allPermutations = set([permutations.combine_numbers(x) for x in permutations.permutations(range(1, 10))]) highestNumber = 0 for n in xrange(1, 10000): # the was found experimentally pandigitalMultiply = 0 for i in xrange(1, 10): pandigitalPart = n * i pandigitalMultiply *= (10 ** len(str(pandigitalPart))) pandigitalMultiply += pandigitalPart if len(str(pandigitalMultiply)) >= 9: break if pandigitalMultiply in allPermutations: if pandigitalMultiply > highestNumber:
- d4 d5 d6 = 635 is divisible by 5 - d5 d6 d7 = 357 is divisible by 7 - d6 d7 d8 = 572 is divisible by 11 - d7 d8 d9 = 728 is divisible by 13 - d8 d9 d10 = 289 is divisible by 17 Find the sum of all 0 to 9 pandigital numbers with this property. """ import permutations allPermutations = permutations.permutations(range(0, 10)) divisibleBy = [2, 3, 5, 7, 11, 13, 17] sumOfPermutations = 0 def is_special_number(numbers): i = 1 for d in divisibleBy: if permutations.combine_numbers(numbers[i : (i + 3)]) % d != 0: return False i += 1 return True for perm in allPermutations: if is_special_number(perm): sumOfPermutations += permutations.combine_numbers(perm) print str(sumOfPermutations)
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 collections import deque import primes import permutations allPrimes = primes.create_primes(1000*1000) countOfCircularPrimes = 0 for prime in allPrimes: isCircularPrime = True digits = deque(permutations.split_into_digits(prime)) for i in xrange(0, len(digits)): digits.rotate(1) newPrime = permutations.combine_numbers(digits) if newPrime not in allPrimes: isCircularPrime = False break if isCircularPrime: countOfCircularPrimes += 1 print str(countOfCircularPrimes)
The product 7254 is unusual, as the identity, 39 x 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital. Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital. HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum. """ import permutations foundProducts = {} foundProductsSum = 0 for perm in permutations.permutations(range(1, 10)): product = permutations.combine_numbers(perm[-4:]) # last four digits for x in xrange(1, 4): factor1 = perm[:x] # first x digit factor2 = perm[x:-4] # starting from x'th digit + 1 (0-based index) all except the last four factor1 = permutations.combine_numbers(factor1) factor2 = permutations.combine_numbers(factor2) if factor1 * factor2 == product: if product not in foundProducts: foundProductsSum += product foundProducts[product] = 1 #print str(factor1) + " x " + str(factor2) + " = " + str(product) print str(foundProductsSum)