Пример #1
0
def prime_pairs(prime):
    # Return the primes lower than the given prime that meet the concat
    # requirement.
    return {paired_prime for paired_prime in primes.range(prime - 1)
            if meets_concat_requirement(prime, paired_prime)}
Пример #2
0
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 sequence import consume
from prime import primes

PANDIGITAL_SETS = {n: frozenset(str(i) for i in range(1, n + 1))
                   for n in range(1, 10)}


def is_pandigital(num):
    string = str(num)
    return set(string) == PANDIGITAL_SETS[len(string)]


# The largest possible pandigital number is 987,654,321.  However no nine-digit
# pandigital number is prime, as the sum of the digits 1-9 is 45, so all such
# numbers can be divided by 9.  The same applies to eight-digit pandigital
# numbers.
#
# So a sketch design presents itself: generate primes up to 7,654,321, then
# return the largest of these that is also pandigital.

if __name__ == '__main__':
    consume(primes.range(7654321))
    print(next(prime for prime in reversed(primes.history)
               if is_pandigital(prime)))
Пример #3
0
        return []

    solutions = []
    for prime in s2:
        solutions.extend(recurse(goal, s1 | {prime}, s2 & pairs[prime], pairs))
    return solutions

if __name__ == '__main__':
    try:
        goal = int(sys.argv[1])
    except IndexError:
        goal = 5

    pairs = {}
    answer = sys.maxsize  # Well above the actual result, so we can use `min`
    for prime in primes.range(3, None):
        if prime > answer:
            # This prime is greater than the best answer we have, so this prime
            # and higher primes cannot possibly produce a better answer.
            break
        pairs[prime] = prime_pairs(prime)
        results = recurse(goal, {prime}, pairs[prime], pairs)
        if results:
            # It turns out the first answer this algorithm produces is correct,
            # but it doesn't take very long to verify this, so we don't break
            # out of the loop until we're actually confident the answer is
            # right.
            answer = min([answer] + [sum(result) for result in results])

    print(answer)
Пример #4
0
def prime_triples():
    for prime_one in primes.range(1000, 9999):
        for prime_two in primes.range(prime_one + 1, 9999):
            if prime_two + (prime_two - prime_one) in primes:
                # Arithmetic sequence of primes.
                yield prime_one, prime_two, prime_two + (prime_two - prime_one)
Пример #5
0
stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797,
379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to
right and right to left.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
'''

from prime import primes


def truncatable_prime(prime):
    string = str(prime)
    for i in range(1, len(string)):
        if int(string[i:]) not in primes:
            return False
        if int(string[:-i]) not in primes:
            return False
    return True

if __name__ == '__main__':
    truncatable_primes = []
    for prime in primes.range(10, None):
        if truncatable_prime(prime):
            truncatable_primes.append(prime)
            if len(truncatable_primes) >= 11:
                break

    print(sum(truncatable_primes))
Пример #6
0
also prime.

What is the largest n-digit pandigital prime that exists?
'''

from prime import primes

PANDIGITAL_SETS = {
    n: frozenset(str(i) for i in range(1, n + 1))
    for n in range(1, 10)
}


def is_pandigital(num):
    string = str(num)
    return set(string) == PANDIGITAL_SETS[len(string)]


# The largest possible pandigital number is 987,654,321.  However no nine-digit
# pandigital number is prime, as the sum of the digits 1-9 is 45, so all such
# numbers can be divided by 9.  The same applies to eight-digit pandigital
# numbers.
#
# So a sketch design presents itself: generate primes up to 7,654,321, then
# return the largest of these that is also pandigital.

if __name__ == '__main__':
    print(
        next(prime for prime in reversed(list(primes.range(7654321)))
             if is_pandigital(prime)))
Пример #7
0
def prime_triples():
    for prime_one in primes.range(1000, 9999):
        for prime_two in primes.range(prime_one + 1, 9999):
            if prime_two + (prime_two - prime_one) in primes:
                # Arithmetic sequence of primes.
                yield prime_one, prime_two, prime_two + (prime_two - prime_one)
Пример #8
0
    # False, True, True, False).
    for replacements in product((True, False), repeat=len(prime_string)):
        # Skip if we're replacing all or none of the digits, since that's not
        # interesting, and also skip if not all the digits being replaced are
        # equal, since in that case the prime we're looking at isn't one of the
        # primes we'll generate.
        if (all_equal(replacements) or
                not all_equal(compress(prime_string, replacements))):
            continue

        # Allow replacement with 0 only if the first digit isn't one that's
        # being replaced.
        if replacements[0]:
            replacement_digits = '123456789'
        else:
            replacement_digits = '0123456789'

        if sum(int(replace_digits(prime_string, replacements, digit)) in primes
               for digit in replacement_digits) >= 8:
            return True

    # Tried all the possible ways to replaced digits without finding a
    # solution.
    return False

if __name__ == '__main__':
    for prime in primes.range(56004, None):
        if test_prime(prime):
            print(prime)
            break
Пример #9
0
379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to
right and right to left.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
'''

from prime import primes


def truncatable_prime(prime):
    string = str(prime)
    for i in range(1, len(string)):
        if int(string[i:]) not in primes:
            return False
        if int(string[:-i]) not in primes:
            return False
    return True


if __name__ == '__main__':
    truncatable_primes = []
    for prime in primes.range(10, None):
        if truncatable_prime(prime):
            truncatable_primes.append(prime)
            if len(truncatable_primes) >= 11:
                break

    print(sum(truncatable_primes))
Пример #10
0
#!/usr/bin/env python3
'''
Summation of primes

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
'''

from sys import argv

from prime import primes

if __name__ == '__main__':
    try:
        target = int(argv[1])
    except IndexError:
        target = 2000000
    print(sum(primes.range(target + 1)))