Ejemplo n.º 1
0
def largest_pandigital_prime():
    maximum = 7654321  # 8- and 9-digit pandigital primes are divisible by 3
    primes = sieve_of_eratosthenes(maximum)
    for number in sorted(primes, reverse=True):
        number_string = ''.join(sorted(str(number)))
        number_length = len(number_string)
        if ''.join(sorted(str(number))) == '1234567'[:number_length]:
            return number
Ejemplo n.º 2
0
def circular_primes(maximum):
    primes = sieve_of_eratosthenes(maximum)
    result = set([2])
    for number in range(3, maximum, 2):
        if number not in primes or number in result:
            continue
        number_string = str(number)
        rotations = [
            int(number_string[i:] + number_string[:i])
            for i in range(len(number_string))
        ]
        if len([p for p in rotations if p in primes]) == len(rotations):
            result.update([p for p in rotations if p < maximum])
    return result
Ejemplo n.º 3
0
def prime_permutations(number_of_digits):
    minimum = 10**(number_of_digits - 1)
    maximum = 10 * minimum
    primes = sieve_of_eratosthenes(maximum)
    permutation_list = []
    permutations_accounted_for = set()
    for prime in [p for p in range(minimum, maximum) if p in primes]:
        if prime in permutations_accounted_for:
            continue
        digit_permutations = set([
            int(''.join(permutation))
            for permutation in permutations(str(prime), number_of_digits)
        ])
        prime_digit_permutations = [
            p for p in digit_permutations
            if p in primes and prime <= p < maximum
        ]
        permutations_accounted_for.update(prime_digit_permutations)
        permutation_list.append(sorted(prime_digit_permutations))
    return [
        permutation for permutation in permutation_list if len(permutation) > 2
    ]
Ejemplo n.º 4
0
def most_consecutive_prime_sum(maximum):
    primes = sieve_of_eratosthenes(maximum)
    prime_list = list(primes)
    prime_set = set(primes)
    number_of_primes_in_sum = result_sum_prime = total = 0

    for (starting_index, starting_prime) in enumerate(prime_list):
        next_prime_index = starting_index + number_of_primes_in_sum
        total = sum(prime_list[starting_index:next_prime_index])

        if total > maximum:
            break

        for (index, prime) in list(enumerate(prime_list))[next_prime_index:]:
            if total > maximum:
                break

            if total in prime_set:
                result_sum_prime = total
                number_of_primes_in_sum = index - starting_index

            total += prime

    return result_sum_prime
Ejemplo n.º 5
0
def summation_of_primes(maximum):
    return sum(sieve_of_eratosthenes(maximum))