Пример #1
0
def problem49():
    """
    The arithmetic sequence, 1487, 4817, 8147, in which each of the terms
    increases by 3330, is unusual in two ways: (i) each of the three terms
    are prime, and, (ii) each of the 4-digit numbers are permutations of
    one another.

    There are no arithmetic sequences made up of three 1-, 2-, or 3-digit
    primes, exhibiting this property, but there is one other 4-digit
    increasing sequence.

    What 12-digit number do you form by concatenating the three terms in
    this sequence?
    """
    limit = 10000
    primes, primeset = util.primes(limit)

    for a in primes:
        permset = set([int(x) for x in util.permutations(str(a))])
        for b in util.permutations(str(a)):
            b = int(b)
            if b > a and b in primeset:
                c = b + (b-a)
                if c in primeset and c in permset and c != 8147:
                    return "{}{}{}".format(a, b, c)
Пример #2
0
def problem43():
    """
    The number, 1406357289, is a 0 to 9 pandigital number because it is made
    up of each of the digits 0 to 9 in some order, but it also has a rather
    interesting sub-string divisibility property.

    d2d3d4=406 is divisible by 2
    d3d4d5=063 is divisible by 3
    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.
    """
    factors = 2, 3, 5, 7, 11, 13, 17
    total = 0
    for digits in util.permutations('0123456789'):
        for i in xrange(1, 8):
            if int(digits[i:i+3]) % factors[i-1] != 0:
                break
        else:
            total += int(digits)
    return total
Пример #3
0
def problem41():
    """
    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?
    """
    from random import randint

    def is_prime(n):
        if pow(2, n, n) != 2:
            return False
        for i in xrange(50):
            a = randint(2, n-1)
            if util.gcd(n, a) == 1 and pow(a, n-1, n) != 1:
                return False
        return True

    best = 2143
    for j in xrange(1, 10):
        for i in util.permutations(''.join(map(str, xrange(1, j+1)))):
            i = int(i)
            if i > best:
                if is_prime(i):
                    best = i
    return best
Пример #4
0
def problem24():
    """
    What is the millionth lexicographic permutation of the digits
    0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
    """
    limit = 1000000
    permute = util.permutations('0123456789')
    for j in xrange(limit - 1):
        next(permute)
    return next(permute)
Пример #5
0
def problem32():
    """
    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, the 5-digit number, 15234,
    is 1 through 5 pandigital.

    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.
    """
    product_set = set()
    for p in util.permutations('123456789'):
        for i in xrange(1, len(p) / 3 + 1):
            for j in xrange(i + 1, len(p) / 3 * 2 + 1):
                triplet = int(p[:i]), int(p[i:j]), int(p[j:])
                if triplet[0] * triplet[1] == triplet[2]:
                    product_set.add(triplet[2])
    return sum(product_set)
Пример #6
0
def prime_perms(r):
    for perm in permutations(range(1, r)):
        n = int(''.join(str(c) for c in perm))
        if is_prime(n):
            yield n
Пример #7
0
if len(sys.argv) != 2:
    usage()

try:
    n = int(sys.argv[1])
except ValueError:
    usage()

l = len(tokens)
limit = 2**18

if n < 0 or n > l:
    usage()

if factorial(l) / factorial(l-n) <= limit:
    for s in util.permutations(n, tokens):
        print(''.join(s))

else:
    strings = set()

    while len(strings) < limit//2:
        new = ''
        choices = list(tokens)
        for i in range(n):
            token = random.choice(choices)
            choices.remove(token)
            new += token
        if tokens[0] in new:
            strings.add(new)