示例#1
0
 def truncatable_prime(number):
     if number < 10:
         return False
     number = str(number)
     length = len(number)
     for i in range(length):
         if not is_prime(int(number[i:])) or not is_prime(
                 int(number[:length - i])):
             return False
     return True
示例#2
0
def p41():
    """ Pandigital prime
    We shall say that an n-digit number is pandigital if it makes use of all the 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?
    """
    import sys
    sys.path.append("../idea bag/")
    from prime_factors import is_prime
    from itertools import permutations

    # EVERY 9 and 8 digit pandigital number is divisible by 3 as the sum of its digits is 45 and 36
    # Thus the search starts with 7 digits
    result = 0
    # Generate all posible permutations of [1..n] for n in [7..1]
    for digits in range(7, 1, -1):
        perms = permutations(str(d) for d in range(1, digits + 1))
        # Reverse sort perms and check for primes
        for perm in sorted(perms, reverse=True):
            if is_prime(int(''.join(perm))):
                result = int(''.join(perm))
                break
        else:
            continue  # executed if the loop ended normally (no break)
        break  # executed if 'continue' was skipped (break)

    return "Largest pandigital prime: {}".format(result)
示例#3
0
 def odd_composite_generator():
     """ Odd non-prime number."""
     n = 1
     while True:
         n += 2
         if not is_prime(n):
             yield n
示例#4
0
def p27():
    """ Quadratic primes
    Euler discovered the remarkable quadratic formula:
    n²+n+41
    It turns out that the formula will produce 40 primes for the consecutive 
    integer values 0≤n≤39. However, when n=40, 40²+40+41 = 40(40+1)+41 is divisible by 41, 
    and certainly when n=41, 41²+41+41 is clearly divisible by 41.

    The incredible formula n²−79n+1601 was discovered, which produces 80 primes 
    for the consecutive values 0≤n≤79. The product of the coefficients, −79 and 1601, is −126479.

    Considering quadratics of the form
    n²+an+b, where |a|<1000 and |b|≤1000

    Find the product of the coefficients, a and b, for the quadratic expression that produces 
    the maximum number of primes for consecutive values of n, starting with n=0.
    """
    # BRUTE FORCE
    import sys
    sys.path.append("../idea bag/")
    from prime_factors import is_prime

    def quadratic(a, b, n):
        return n * n + a * n + b

    ab_values = [(a, b) for a in range(-1000, 1000)
                 for b in range(-1000, 1000)]
    n = 0
    while True:
        # print(n, len(ab_values))
        # new_ab_values = []
        # for ab in ab_values:
        #     if len(ab_values) <= 1:
        #         (a, b) = ab
        #         return n, 'n²{:+}n{:+}={}'.format(*ab, quadratic(*ab, n)), 'a*b={}'.format(a * b)
        #     if is_prime(quadratic(*ab, n)):
        #         new_ab_values.append(ab)
        # ab_values = new_ab_values

        # OPTIMIZED
        ab_values = [ab for ab in ab_values if is_prime(quadratic(*ab, n))]

        if len(ab_values) <= 1:
            (a, b) = ab_values[0]
            return ('Consecutive primes:{}'.format(n), 'n²{:+}n{:+}={}'.format(
                a, b, quadratic(a, b, n)), 'a*b={}'.format(a * b))
        n += 1
示例#5
0
def p50():
    """ Consecutive prime sum
    The prime 41, can be written as the sum of six consecutive primes:

    41 = 2 + 3 + 5 + 7 + 11 + 13
    This is the longest sum of consecutive primes that adds to a prime below one-hundred.

    The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms,
    and is equal to 953.

    Which prime, below one-million, can be written as the sum of the most consecutive primes?
    """
    import sys
    sys.path.append("../idea bag/")
    from prime_factors import sieve_of_eratosthenes
    from itertools import accumulate, islice

    max_number = 1000000

    primes = sieve_of_eratosthenes(max_number)
    primes_hash = set(primes)

    # Slice primes sequence necesary for accumulation
    *_, end_sequence = (i for i, _ in enumerate(accumulate(primes)))
    primes_sequences = primes[:end_sequence]

    # acc = 0
    # for prime in primes:
    #     acc += prime
    #     if acc > 0:
    #         pass

    def is_prime(number):
        return number in primes_hash

    for length in range(len(primes), 1, -1):
        # print("length: ", length)
        for start in range(len(primes) - length):
            # print("start: ", start)
            sequence = primes[start:start + length]
            acc = sum(sequence)
            if acc > max_number:
                break
            if is_prime(acc):
                # print(len(sequence))
                return acc
示例#6
0
def p35():
    """ Circular primes
    The number, 197, is called a circular prime because all rotations of the digits: 
    197, 971, and 719, are themselves prime.
    There are thirteen such primes below 100: 
    2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

    How many circular primes are there below one million?
    """
    import sys
    sys.path.append("../idea bag/")
    from prime_factors import is_prime

    def rotations(number):
        number = str(number)
        digits = len(number)
        return tuple(int(number[i:] + number[:i]) for i in range(digits))

    return len([
        None for number in range(1000000) if all(
            is_prime(n) for n in rotations(number))
    ])