Beispiel #1
0
    def execute(self):
        answer = None

        truncatable_primes = []
        num = 11

        while len(truncatable_primes) < 11:
            if '0' not in str(num) and '5' not in str(num)[1:]:
                if is_prime_naive(num):
                    is_truncatable_prime = True
                    temp = str(num)

                    for index in range(1, len(temp)):
                        left_truncated = int(temp[index:])
                        right_truncated = int(temp[:index])

                        if not is_prime_naive(left_truncated) or not is_prime_naive(right_truncated):
                            is_truncatable_prime = False
                            break

                    if is_truncatable_prime:
                        truncatable_primes.append(num)

            num += 2

        answer = sum(truncatable_primes)

        return answer
Beispiel #2
0
    def execute(self):
        answer = None

        primes = smaller_primes(1000000)
        filtered_primes = [prime for prime in primes if '0' not in str(prime) and '5' not in str(prime)]
        circular_primes = set([5])

        for num in filtered_primes:
            if num not in circular_primes:
                is_circular_prime = True
                circular_prime_candidates = set([num])
                temp = str(num)

                for index in range(1, len(temp)):
                    rotated_num = int(temp[index:] + temp[:index])

                    if is_prime_naive(rotated_num):
                        circular_prime_candidates.add(rotated_num)
                    else:
                        is_circular_prime = False
                        break

                if is_circular_prime:
                    circular_primes |= circular_prime_candidates

        answer = len(circular_primes)

        return answer
Beispiel #3
0
    def execute(self):
        answer = None

        limit = 1000000
        n = 1

        while True:
            approximate_value = n**2 * log(n) / 2

            if approximate_value >= limit:
                break

            n += 1

        primes = first_primes(n)

        for length in range(n, 0, -1):
            for start_index in range(0, n - length + 1):
                end_index = start_index + length
                consecutive_sum = sum(primes[start_index:end_index])

                if consecutive_sum < limit and is_prime_naive(consecutive_sum):
                    answer = consecutive_sum
                    break

            if answer is not None:
                break

        return answer
Beispiel #4
0
    def execute(self):
        answer = None

        number_count = 1
        prime_count = 0
        distance_from_origin = 0

        while answer is None:
            distance_from_origin += 1

            current_square_length = 2 * distance_from_origin + 1
            inner_square_length = current_square_length - 2

            diagonal_gap = current_square_length - 1
            min_diagonal = inner_square_length**2 + diagonal_gap
            max_diagonal = current_square_length**2

            number_count += 4

            for num in range(min_diagonal, max_diagonal + 1, diagonal_gap):
                if is_prime_naive(num):
                    prime_count += 1

            if prime_count / number_count < 0.1:
                answer = current_square_length

        return answer
Beispiel #5
0
    def execute(self):
        answer = None

        num = 9

        while answer is None:
            if not is_prime_naive(num):
                is_writable_as_sum = False
                base_limit = floor(sqrt((num - 3) / 2))

                for square_base in range(1, base_limit + 1):
                    if is_prime_naive(num - 2 * square_base**2):
                        is_writable_as_sum = True
                        break

                if not is_writable_as_sum:
                    answer = num

            num += 2

        return answer
Beispiel #6
0
    def execute(self):
        answer = 0

        for pandigital_length in [4, 5, 7, 8]:
            digits = [str(num) for num in range(1, pandigital_length + 1)]

            for order in range(1, factorial(pandigital_length) + 1):
                permutation = int(nth_lexicographic_permutation(order, digits))

                if is_prime_naive(permutation) and answer < permutation:
                    answer = permutation

        return answer
Beispiel #7
0
    def execute(self):
        answer = None

        prime_count_per_pattern = {}
        num = 9

        while answer is None:
            num += 2

            if not is_prime_naive(num):
                continue

            temp = str(num)
            unique_digits = set(temp[:-1])

            for digit in unique_digits:
                generating_pattern = temp[:-1].replace(digit, '*') + temp[-1:]

                if generating_pattern in prime_count_per_pattern:
                    continue

                prime_count = 0

                for substitute in '0123456789':
                    decimal = generating_pattern.replace('*', substitute)

                    if decimal[0] == '0':
                        continue

                    if is_prime_naive(int(decimal)):
                        prime_count += 1

                if prime_count == 8:
                    answer = num
                    break

                prime_count_per_pattern[generating_pattern] = prime_count

        return answer
Beispiel #8
0
        def find_prime_pair_set(base_connectable_primes, other_primes):
            for index, another_prime in enumerate(other_primes):
                is_all_prime = True

                for connectable_prime in base_connectable_primes:
                    forward_connected = int(
                        f'{connectable_prime}{another_prime}')
                    reverse_connected = int(
                        f'{another_prime}{connectable_prime}')

                    if forward_connected not in prime_dict:
                        prime_dict[forward_connected] = is_prime_naive(
                            forward_connected)

                    if reverse_connected not in prime_dict:
                        prime_dict[reverse_connected] = is_prime_naive(
                            reverse_connected)

                    if not prime_dict[forward_connected] or not prime_dict[
                            reverse_connected]:
                        is_all_prime = False
                        break

                if is_all_prime:
                    extended_connectable_primes = [
                        *base_connectable_primes, another_prime
                    ]

                    if len(base_connectable_primes) == prime_set_size - 1:
                        return extended_connectable_primes
                    else:
                        more_smaller_primes = other_primes[index + 1:]

                        prime_pair_set = find_prime_pair_set(
                            extended_connectable_primes, more_smaller_primes)
                        if prime_pair_set is not None:
                            return prime_pair_set

            return None
Beispiel #9
0
    def execute(self):
        answer = None

        prime_dict = {
            2: True,
        }
        prime_set_size = 5

        def find_prime_pair_set(base_connectable_primes, other_primes):
            for index, another_prime in enumerate(other_primes):
                is_all_prime = True

                for connectable_prime in base_connectable_primes:
                    forward_connected = int(
                        f'{connectable_prime}{another_prime}')
                    reverse_connected = int(
                        f'{another_prime}{connectable_prime}')

                    if forward_connected not in prime_dict:
                        prime_dict[forward_connected] = is_prime_naive(
                            forward_connected)

                    if reverse_connected not in prime_dict:
                        prime_dict[reverse_connected] = is_prime_naive(
                            reverse_connected)

                    if not prime_dict[forward_connected] or not prime_dict[
                            reverse_connected]:
                        is_all_prime = False
                        break

                if is_all_prime:
                    extended_connectable_primes = [
                        *base_connectable_primes, another_prime
                    ]

                    if len(base_connectable_primes) == prime_set_size - 1:
                        return extended_connectable_primes
                    else:
                        more_smaller_primes = other_primes[index + 1:]

                        prime_pair_set = find_prime_pair_set(
                            extended_connectable_primes, more_smaller_primes)
                        if prime_pair_set is not None:
                            return prime_pair_set

            return None

        ordered_primes = [2]
        num = 3

        while answer is None:
            if num not in prime_dict:
                prime_dict[num] = is_prime_naive(num)

            if prime_dict[num]:
                smaller_primes = ordered_primes[:]
                ordered_primes = [num] + smaller_primes
                prime_pair_set = find_prime_pair_set([num], smaller_primes)

                if prime_pair_set is not None:
                    answer = sum(prime_pair_set)

            num += 2

        return answer