示例#1
0
def find_primes(*, up_to=100):
    primes = []
    for p in prime_number_generator():
        if p >= up_to:
            break
        primes.append(p)
    return primes
示例#2
0
def primes_range(start, stop):
    primes = []
    for p in takewhile(
            lambda x: x < stop,
            dropwhile(lambda y: y <= start, prime_number_generator())):
        primes.append(p)
    return primes
示例#3
0
def run_exercise(*, n=6):
    primes = []
    for p in prime_number_generator():
        primes.append(p)
        n -= 1
        if not n > 0:
            break
    print('Solution =', primes[-1])
示例#4
0
def run_exercise(*, limit=10):
    prime = prime_number_generator()
    primes = []
    while True:
        p = next(prime)
        if p >= limit:
            break
        primes.append(p)
    print(sum(primes))
示例#5
0
def attempt():
    limit = 10**6
    prime = 0
    terms = 1
    prime_sums = [0]
    for p in prime_number_generator():
        prime_sums.append(prime_sums[-1] + p)
        if prime_sums[-1] >= limit:
            break
    candidates = []
    for j in range(len(prime_sums)):
        for k in range(j):
            candidate = prime_sums[j] - prime_sums[k]
            if is_prime(candidate):
                candidates.append((candidate, j - k))
                break
    return max(candidates, key=lambda x: x[terms])[prime]
示例#6
0
def run_solution(n=13195):

    prime = prime_number_generator()

    factors = []

    p = next(prime)
    while n > 1:
        while (n % p) == 0:
            n //= p
            factors.append(p)
        p = next(prime)

    counter = Counter(factors)

    print('all prime factors:', counter)
    print('maximum prime factor:', max(counter))
示例#7
0
def attempt():

    max_truncatable_primes = 11
    truncatable_primes = []

    for prime in prime_number_generator():

        if len(truncatable_primes) >= max_truncatable_primes:
            break

        if prime < 10:
            continue

        trunks = find_all_trunks(prime)

        if all(map(is_prime, trunks)):
            print('prime =', prime, ', trunks =', list(sorted(trunks)))
            truncatable_primes.append(prime)

    return sum(truncatable_primes)
示例#8
0
def attempt():
    """Builds a product from consecutive ascending primes until we not longer exceed the limit.
    Then goes back by dividing the denominator so far by the last prime, and then stepping up
    by multiplying by two (i.e. growing the denominator the smallest possible step) for
    several rounds.
    CAVEAT! To be sure really having found the smallest denominator, some testing and
    optimizing is necessary. There might still be smaller denominators in the wild.
    """
    limit = Fraction(15499, 94744)
    # limit = Fraction(4, 10)
    prime = prime_number_generator()
    denominator = next(prime)
    while denominator_resilience(denominator) >= limit:
        p = next(prime)
        denominator *= p
    denominator //= p
    # prime = prime_number_generator()
    while denominator_resilience(denominator) >= limit:
        # denominator *= next(prime)
        denominator *= 2
    print('R({}) = {} and is less than {}.'.format(
        denominator, denominator_resilience(denominator), limit))