Пример #1
0
def gen_odd_composites():
    step_size = 10000
    sieve_primes(step_size)  # Cache primes

    # Avoiding lower limit. This avoids running an unnecessary filter for each caching step
    for j in range(9, step_size + 1, 2):
        if not is_prime(j):
            yield j

    for i in count(2):
        sieve_primes(i * step_size + 1)  # Cache primes
        for j in range(step_size * (i - 1) + 1, step_size * i + 1, 2):
            if not is_prime(j):
                yield j
Пример #2
0
def solve_by_pairing(target_size=target_set_size):
    # This algorithm will populate prime_pairs with each prime (key) pointing to a set of all
    #   smaller primes that form a valid prime pair with it.
    # Each of these is then checked by get_connected_set to see if there are enough pairwise
    #   connections to complete the problem.

    # NOTE: prime index starts from 3 to avoid primes 2 and 5, which cannot be part of any prime pair.
    # 3 is chained into the inner loop, but not the outer loop because no valid primes are less than 3.
    log_step = 1000
    next_step = 0

    for p in primes(step=10000, start_index=3):
        for q in chain(reversed(sieve_primes(max_prime=p)[3:-1]), [3]):
            populate_prime_pairs(p, q)

        if p > next_step:
            split_timer()
            print(f"Reached p > {next_step} -- {p}: {prime_pairs.get(p)}")
            next_step += log_step

        if p in prime_pairs:
            p_set = get_connected_set(prime_pairs[p], target_size - 1)
            if p_set:
                p_set.add(p)
                return sorted(p_set)[:target_size]
Пример #3
0
def test_sieve_primes_max_6_upper_edge():
    """
    Test whether :param max_prime: result includes the given value.

    NOTE: Omitted clear_prime_cache teardown, which conflates two edge case tests due to result of previous test
        (:func test_sieve_primes_max_6_lower_edge:). I.e., before generating the expected 6th prime, sieve_primes
        must first recognize that the cache has not been sufficiently updated for the given value.
    """
    assert prime.sieve_primes(max_prime=primes[6]) == primes[:7]
Пример #4
0
def test_sieve_primes(clear_prime_cache):
    assert prime.sieve_primes(num_primes=100) == primes, "Failed to sieve first 100 primes."
Пример #5
0
def test_sieve_primes_max_6_lower_edge(clear_prime_cache):
    """Test whether :param max_prime: result does not exceed the given value."""
    assert prime.sieve_primes(max_prime=primes[6] - 1) == primes[:6]
Пример #6
0
d3d4d5=063 is divisible by 3
d4d5d6=635 is divisible by 5
d5d6d7=357 is divisible by 7
d6d7d8=572 is divisible by 11
d7d8d9=728 is divisible by 13
d8d9d10=289 is divisible by 17
Find the sum of all 0 to 9 pandigital numbers with this property.
"""
import time

from prime import sieve_primes
from seq import pandigitals

start_time = time.time()

primes = sieve_primes(num_primes=7)


def substring_divisible(n, divisors=primes):
    n_str = str(n)
    for i in range(1, len(n_str) - 2):
        if int(n_str[i:i + 3]) % divisors[i - 1] != 0:
            return False
    return True


total = 0

for p in pandigitals(9, include_zero=True):
    if substring_divisible(p):
        total += p
Пример #7
0
"""
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10,001st prime number?
"""
from datetime import datetime

from common import elapsed
from prime import sieve_primes

start_time = datetime.now()

limit = 10001

print("Prime {}: {}".format(limit, sieve_primes(num_primes=limit)[-1]))
elapsed()
Пример #8
0
"""
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?
"""
from prime import sieve_primes
import time

start_time = time.time()

limit = 1000000

# Will be removing primes from this as it is traversed to avoid duplicate work
primes_filtered = set(sieve_primes(max_prime=limit))


def get_rotations(n):
    n_str = str(n)
    return set(int(n_str[m:] + n_str[:m]) for m in range(len(n_str)))


def is_prime(n):
    if n in primes_filtered:
        return True


count = 0

print("Testing {} primes less than {}".format(len(primes_filtered), limit))
Пример #9
0
"""
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.
"""
from datetime import datetime

from common import elapsed
from prime import sieve_primes

start_time = datetime.now()

max_prime = 2000000
print(sum(sieve_primes(max_prime=max_prime)))
elapsed()
Пример #10
0
"""
import time

from prime import is_prime, sieve_primes

start_time = time.time()

coefficient_limit = 1000


def count_primes_from_quadratic(a, b):
    n = 0
    while is_prime(n**2 + a * n + b):
        n += 1
    return n


best_ab = None
seq_len = 0

for b in sieve_primes(max_prime=coefficient_limit):
    for a in range(-coefficient_limit, coefficient_limit + 1):
        count = count_primes_from_quadratic(a, b)
        if count > seq_len:
            seq_len = count
            best_ab = (a, b)

print("Best pair is {}, {} (generated {} primes), giving: {}".format(
    *best_ab, seq_len, best_ab[0] * best_ab[1]))
print("Execution time: {}".format(time.time() - start_time))