Esempio n. 1
0
def euler():
    # set of the digits to use for generating numbers: all the digits except
    # '1', see related hack
    digits = list(set(range(10)) - {1})
    # number of matches found, only take the second one
    matches_count = 0
    # for each permutation of the digits
    for digit_permutation in sequence.permutations(digits):
        # for each truncation in that permutation
        for base_digits in sequence.right_truncations(digit_permutation):
            # add '1' at the beginning of the number, because the number
            # must start with a '1' in order to have the same number of digits
            # when multiplied by 6
            base_digits = [1] + base_digits
            # the base number which will be multiplied
            base = int(''.join(str(digit) for digit in base_digits))
            # found will stay True if all the multiplications by the FACTORS
            # have the same digits
            found = True
            # for each factor, check if it yields the same digits
            for factor in FACTORS:
                found = found and set(str(base)) == set(str(base * factor))
            # if it's a valid answer
            if found:
                # return the second valid answer found
                matches_count += 1
                if matches_count == 2:
                    return base
Esempio n. 2
0
def euler():
    products_cache = {}
    accumulator = 0
    for permutation in sequence.permutations('123456789'):
        permutation = ''.join(permutation)
        products = valid_products(permutation)
        for product in products:
            if not product in products_cache:
                accumulator += product
                products_cache [product] = True
    return accumulator
Esempio n. 3
0
def euler():
    # list of digits
    tokens = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    # current permutation counter
    index = 0
    # for each permutation of the digits
    for permutation in sequence.permutations(tokens):
        index += 1
        # if the permutation is the one we want
        if index == ANSWER_INDEX:
            # return it, as a string
            return ''.join(map(str, permutation))
Esempio n. 4
0
def euler():
    # highest pandigital prime found so far
    highest_pandigital = 0
    # for number of digits from 4 to 9
    for length in range(4, 9):
        # generate the list of digits from 1 to n
        digits = list(range(1, length + 1))
        # for each permutation of these digits
        for permutation in sequence.permutations(digits):
            number = int(''.join(str(digit) for digit in permutation))
            # check if the number is prime
            if prime.is_prime(number):
                # set the new value of the highest pandigital prime if needed
                highest_pandigital = max(number, highest_pandigital)
    # return the highest pandigital prime found
    return highest_pandigital