Exemple #1
0
def refined_brute_force_solution():
    """
    Iterate throughe very combination of a/b and compute the number of consecutive primes.

    Use some basic math to reduce the search space for both a and b.
    """
    most_consecutive_primes = 0
    most_consectuive_primes_product = 0

    # given the quadratic formula, b must be prime otherwise the condition where n=0 will not
    # produce a prime
    b_ints = [i for i in range(1001) if utils.is_prime(i)]

    # since we are only considering b values which are primes, the condition where n=1 produces
    # 1 + a - b. In order for this to be prime, a must be an odd number otherwise the resulting
    # value will be even and not prime for n=1
    for a in range(-999, 1000, 2):
        for b in b_ints:
            # starting at n=0 the number of consecutive primes is equal to the index of the first
            # non-prime
            consecutive_primes = next(n for n in itertools.count()
                                      if not utils.is_prime(n**2 +
                                                            (a * n) + b))

            if consecutive_primes > most_consecutive_primes:
                most_consecutive_primes = consecutive_primes
                most_consectuive_primes_product = a * b

    return most_consectuive_primes_product
Exemple #2
0
def prime_numbers():
    """Generator expression which returns sequence of prime numbers."""
    yield 2

    for i in itertools.count(3, step=2):
        if utils.is_prime(i):
            yield i
def brute_force_solution():
    """Iterate through every number and test if it and it's rotations are prime."""
    circular_primes = [
        i for i in range(2, LIMIT) if all(
            utils.is_prime(rotation) for rotation in rotations(i))
    ]

    return len(circular_primes)
def brute_force_solution():
    """Search for 11 truncatable primes starting with two digit integers."""
    truncatable_primes = []
    i = 9

    while len(truncatable_primes) < 11:
        i += 2  # skip even numbers...

        i_str = str(i)
        for j in range(1, len(i_str)):
            left_2_right = int(i_str[:j])
            right_2_left = int(i_str[-j:])

            if not utils.is_prime(left_2_right) or not utils.is_prime(
                    right_2_left):
                break
        else:
            # At this point, all subsets of i from left to right and right to left must be prime
            if utils.is_prime(i):
                truncatable_primes.append(i)

    return sum(truncatable_primes)
def reverse_search_solution():
    """
    Search list of pandigital numbers, starting with 9 digit numbers, for the first prime.

    Using a sieve to compute primes up to 9 digits is much more expensive than iterating over
    pandigital number set.

    Iterating from 987654321 to 1 by decrementing is also much more expensive than computing
    permutations of pandigital set since most numbers will not be pandigital.
    """
    return next(
        pandigital_number
        for pandigital_number in reverse_pandigital_iter(max_width=9)
        if utils.is_prime(pandigital_number)
    )
def brute_force_reduce_search_solution():
    """
    Similar approach to original method but reduce the search space.

    We can reduce the set of numbers we need to calculate because we know that any number
    that contains an even digit (0, 2, 4, 6, 8) or a 5 cannot have all rotations be prime
    since one rotation will have (0, 2, 4, 6, 8, 5) in the ones digit and that number is
    always divisible.

    This reduces compute time by roughly 5x.
    """
    circular_primes_over_100 = [
        i for i in circular_prime_search_space(101, LIMIT) if all(
            utils.is_prime(rotation) for rotation in rotations(i))
    ]

    return len(circular_primes_over_100) + 13  # 13 circular primes below 100
def reverse_search_solution_max_seven_digits():
    """
    Use the 3 divisiblity rule to reduce search space for reverse search solution.

    Both reverse search solutions are still much faster than precomputing all primes up to 7
    digits using sieve approach. Rough performance outline:

        reverse_search_solution_max_seven_digits: 1x (~300usec)
        reverse_search_solution: 3,500x
        sieve_solution: 30,000x

    """
    return next(
        pandigital_number
        for pandigital_number in reverse_pandigital_iter(max_width=7)
        if utils.is_prime(pandigital_number)
    )
Exemple #8
0
def odd_composite_numbers():
    """Generator expression which returns sequence of odd composite numbers."""
    for i in itertools.count(9, step=2):
        if not utils.is_prime(i):
            yield i