Example #1
0
def special_sum(up_to_n):
    polling_interval = 1000000
    prime_list = [p for p in primes(int((up_to_n + 1) ** 0.5) + 1)]
    tot = 0
    t0 = time.time()
    for j in xrange(2, up_to_n, 4):
        if is_special(j, prime_list):
            print(j)
            tot += j
        if (j - 2) % polling_interval == 0:
            t = time.time() - t0
            t0 = time.time()
            print("[{:.2f}s, {:,.0f}/s] {:,d} out of {:,d}".format(t, polling_interval / t, j - 2, up_to_n))
    return tot
Example #2
0
def gen_special(up_to_n):
    yield 2
    print("generating list...")
    prime_list = primes(up_to_n / 2)
    print("have primes.  finding max number of factors.")
    max_length = next(j for j, _ in enumerate(prime_list) if prod(prime_list[:j]) > up_to_n)
    print("max factors is {:d}.  Analyzing factors.".format(max_length))
    for j in range(1, max_length + 1):
        print("Analyzing factors of length {:d}".format(j))
        for factors in combinations(prime_list[1:], j):
            print(factors)
            n = 2 * prod(factors)
            if n > up_to_n:
                break
            if all(is_prime(divisor + n / divisor, prime_list) for divisor in divisor_gen_from_factors(list(factors) + [2])):
                yield n
Example #3
0
def divides_small_repunit(primes_to=100):
    prime_list = primes(primes_to)
    found_set = set()
    same_count = 0
    prime_gen = prime_generator()
    while same_count < 5:  # wait for list to stabilize
        found_set_len = len(found_set)
        j = prime_gen.next()
        for p in prime_list:
            if repunit_factor(j, p):
                found_set.add(p)
        if found_set_len == len(found_set):
            same_count += 1
        else:
            same_count = 0
    return sum(prime_list) - sum(found_set)
Example #4
0
def scratch(up_to_n):
    prime_list = [p for p in primes(int((up_to_n + 1) ** 0.5) + 1)]
    t0 = time.time()
    for j in xrange(2, up_to_n, 4):
        if is_special(j, prime_list):
            print(j, list(factor_num(j)))
Example #5
0
def solve(upper_bound):
    tot = 0
    prime_list = primes(int(1.01 * upper_bound))
    for idx, prime in (j for j in enumerate(prime_list) if 5 <= j[1] <= upper_bound):
        tot += connection(*prime_list[idx:idx + 2])
    print tot
# 10001st prime
from utilities.primes import primes

print "10001st prime number is: {}".format(primes(1000000)[10000])
def get_largest_pan_digital_prime():
    prime_nums = primes(10000000)
    return max([x for x in prime_nums if is_pan_digital(x)])
def test_primes():
    assert_list_equal(primes(20), list(small_primes))