Exemple #1
0
# Problem 24
#
# A permutation is an ordered arrangement of objects. For example, 3124
# is one possible permutation of the digits 1, 2, 3 and 4. If all of the
# permutations are listed numerically or alphabetically, we call it
# lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
#
# 012   021   102   120   201   210
#
# What is the millionth lexicographic permutation of the digits 0, 1, 2,
# 3, 4, 5, 6, 7, 8 and 9?

import eutils.strings as strings
import time
start = time.time()

n = 1000000
digits = list('0123456789')

ans = int(strings.l_collapse([strings.l_permute(digits)[n - 1]])[0])

end = time.time()
print("The answer to Problem 24 is: %s" % ans)
print("<< Returned in %s seconds >>" % (end - start))
Exemple #2
0
# Problem 41
#
# We shall say that an n-digit number is pandigital if it makes use of
# all the digits 1 to n exactly once. For example, 2143 is a 4-digit
# pandigital and is also prime.
#
# What is the largest n-digit pandigital prime that exists?

import eutils.primes as primes
import eutils.strings as strings
import time

start = time.time()

m = 9
found = False
while not found:
    digits = list(range(m - 1, 0, -1))
    perms = [int(str(m) + x) for x in strings.l_collapse(strings.l_permute(digits))]
    p = [x for x in perms if primes.is_prime(x)]
    if len(p):
        found = True
    m -= 1


ans = p[0]

end = time.time()
print("The answer to Problem 41 is: %s" % ans)
print("<< Returned in %s seconds >>" % (end - start))
Exemple #3
0
# * d4d5d6=635 is divisible by 5
# * d5d6d7=357 is divisible by 7
# * d6d7d8=572 is divisible by 11
# * d7d8d9=728 is divisible by 13
# * d8d9d10=289 is divisible by 17
#
# Find the sum of all 0 to 9 pandigital numbers with this property.

import eutils.strings as strings
import eutils.vectorize as vec
import math
import time
start = time.time()

n = 10
digits = list(range(0, n))
check = [1,2,3,5,7,11,13,17]

perms = strings.l_permute(digits)[int(math.factorial(n) / n):]
perms = [''.join([str(y) for y in x]) for x in perms]

ans = 0

for s in perms:
    pan = vec.v_sum([int(s[i:i + 3]) % d for i, d in enumerate(check)])
    if pan == 0:
        ans += int(s)

end = time.time()
print("The answer to Problem 43 is: %s" % ans)
print("<< Returned in %s seconds >>" % (end - start))