Beispiel #1
0
def main():
    """
    >>> main()
    16695334890
    """
    base = frozenset(xrange(10))

    result = 0
    for d4 in [0, 2, 4, 6, 8]:  # to be divisible by 2
        d6 = 5  # otherwise d_7 and d_8 must be equal to be divisible by 11

        for div11 in range(506, 605, 11):  # divisible by 11
            _, d7, d8 = int_to_list(div11)

            number = set([d4, d6])
            if d7 in number or d8 in number:
                continue  # not pandigital

            for div17 in range(102, 1003, 17):  # divisible by 17
                if div17 < d8 * 100 or div17 > (d8 + 1) * 100:
                    continue  # d8 doesn't match

                _, d9, d10 = int_to_list(div17)

                number = set([d4, d6, d7, d8])
                if d9 in number or d10 in number:
                    continue  # not pandigital

                if list_to_int([d7, d8, d9]) % 13 != 0:
                    continue  # not divisible by 13

                number = set([d4, d6, d7, d8, d9, d10])
                for d3 in base - number:

                    number = set([d3, d4, d6, d7, d8, d9, d10])
                    for d5 in base - number:

                        if list_to_int([d3, d4, d5]) % 3 != 0:
                            continue  # not divisible by 3

                        if list_to_int([d5, d6, d7]) % 7 != 0:
                            continue  # not divisible by 7

                        number = set([d3, d4, d5, d6, d7, d8, d9, d10])
                        for d2 in base - number:

                            number = set([d2, d3, d4, d5, d6, d7, d8, d9,
                                         d10, 0])
                            for d1 in base - number:
                                result += list_to_int([
                                    d1, d2, d3, d4, d5, d6, d7, d8, d9, d10])

    print(result)
Beispiel #2
0
def main():
    """
    >>> main()
    26033
    """
    primes = Primes(10 ** 8)

    remarkable_primes = []
    remarkable_primes_min_sum = sys.maxsize

    i = 0
    while True:
        if i > PRIME_INDEX_LIMIT:
            last_added_prime = list_to_int(remarkable_primes.pop())
            i = primes.index(last_added_prime) + 1
            if len(remarkable_primes) == 0 and i == PRIME_INDEX_LIMIT:
                break

        new_prime = str(primes[i])
        new_prime_is_remarkable = True
        for prime in remarkable_primes:
            if list_to_int(new_prime + prime) not in primes:
                new_prime_is_remarkable = False
                break

            if list_to_int(prime + new_prime) not in primes:
                new_prime_is_remarkable = False
                break

        if new_prime_is_remarkable:
            remarkable_primes.append(new_prime)
            if len(remarkable_primes) == LEN_PRIME_SET:
                primes_sum = sum((int(p) for p in remarkable_primes))
                if primes_sum < remarkable_primes_min_sum:
                    remarkable_primes_min_sum = primes_sum

        i += 1

    print(remarkable_primes_min_sum)
Beispiel #3
0
def main():
    """
    >>> main()
    8581146
    """
    chains_that_arrive_at_89 = 0
    for number in combinations_with_replacement(xrange(10), 7):
        n = list_to_int(number)
        while n != 1 and n != 89 and n != 0:
            n = sum([d ** 2 for d in int_to_list(n)])

        if n == 89:
            chains_that_arrive_at_89 += count_permutations_of(number)

    print(chains_that_arrive_at_89)
Beispiel #4
0
def main():
    """
    >>> main()
    443839
    """
    power = 5
    limit = 6

    result = (-1)  # 1 ** power isn't included
    for combination in combinations_with_replacement(xrange(10), r=limit):
        summe = sum([d ** power for d in combination])
        for permutation in permutations(combination, limit):
            number = list_to_int(permutation)
            if summe < number:
                break
            elif summe == number:
                result += number
                break

    print(result)
Beispiel #5
0
def main():
    """
    >>> main()
    932718654
    """
    max_len_number = 9
    max_pandigital_number = 0
    # since the multiplier must be at least 2 we can stop when the
    # multiplicand * 1 and multiplicand * 2 together have more than 9 numbers
    for multiplicand in range(2, 10000):
        product_as_list = int_to_list(multiplicand)
        for multiplier in count(2):
            product_as_list += int_to_list(multiplicand * multiplier)
            product = list_to_int(product_as_list)

            if is_int_pandigital(product) and max_pandigital_number < product:
                max_pandigital_number = product
            elif len(product_as_list) > max_len_number:
                break

    print(max_pandigital_number)
Beispiel #6
0
def main():
    """
    >>> main()
    121313
    """
    amount_of_primes = 8

    primes = Primes(10 ** 6)
    for prime in primes:
        prime_as_list = int_to_list(prime)

        list_of_indices = []  # indices of reoccuring digits
        for i in range(len(prime_as_list)):
            for j in range(i + 1, len(prime_as_list)):
                for k in range(j + 1, len(prime_as_list)):
                    if prime_as_list[j] == prime_as_list[k]:
                        list_of_indices.append((i, j, k))

        for indices in list_of_indices:
            prime_family = []
            for d in range(10):
                prime_candidate = prime_as_list[:]
                for i in indices:
                    prime_candidate[i] = d

                # ensure that we don't look at n-1-digit numbers
                if prime_candidate[0] == 0:
                    continue

                prime_candidate = list_to_int(prime_candidate)
                if prime_candidate in primes:
                    prime_family.append(prime_candidate)

                # check if we still can get the neccasry amount of primes
                if (10 - d) + len(prime_family) < amount_of_primes:
                    break

            if len(prime_family) == amount_of_primes:
                print(prime)
                return
Beispiel #7
0
def rotate(number):
    """ return all rotations of this number"""
    digits = int_to_list(number)
    for _ in range(len(digits)):
        yield list_to_int(digits)
        digits.append(digits.pop(0))