예제 #1
0
def main():
    primes = prime_under(10**4)
    primes.remove(5)

    p1, p2, p3, p4, p5 = find_solution5(primes)
    print(p1, p2, p3, p4, p5)
    print(sum([p1, p2, p3, p4, p5]))
예제 #2
0
def main():
    primes = prime_under(10**4)
    primes.remove(5)

    p1, p2, p3, p4, p5 = find_solution5(primes)
    print(p1, p2, p3, p4, p5)
    print(sum([p1, p2, p3, p4, p5]))
예제 #3
0
def main():
    first40 = []
    count = 0
    for p in prime_under(10 ** 6)[3:]:  # exclude 2 and 5, which are not coprime with 10
        if repunit_mod(p) == 0:
            count += 1
            # print(count, p)
            first40.append(p)
            if count == 40:
                break
    # print(first40)
    print(sum(first40))
예제 #4
0
def main():
    first40 = []
    count = 0
    for p in prime_under(
            10**6)[3:]:  # exclude 2 and 5, which are not coprime with 10
        if repunit_mod(p) == 0:
            count += 1
            # print(count, p)
            first40.append(p)
            if count == 40:
                break
    # print(first40)
    print(sum(first40))
예제 #5
0
def decomp_matrix(n):
    primes = prime_under(n + 1)
    primes_exp = [p**(int(log(n, p)) * 2) for p in primes]

    mat = [[0] * (n - 1) for _ in range(len(primes))]

    for i in range(2, n + 1):
        decomp = decompose(i)
        for coeff, fact_exp, fact in decomp:
            idx = primes.index(fact)
            mat[idx][i - 2] = coeff * (primes_exp[idx] // fact_exp)

    return mat, primes_exp
예제 #6
0
def main():
    ceiling = 5 * 10**7
    primes = prime_under(int((ceiling - 2**3 - 2**4)**0.5) + 1)
    nums = set()
    print("Primes generated by:", time() - starting_time, "seconds")

    for i in primes:
        for j in primes:
            for k in primes:
                num = i**4 + j**3 + k**2
                if num > ceiling:
                    break
                nums.add(num)
    print(len(nums))
예제 #7
0
def main():
    N = 4 * 10**6
    N2 = N * 2 + 1
    primes = prime_under(50)
    # print(log(N2, 3), len(primes), list(enumerate(primes)), sep='\n')

    choice = []
    for i3 in range(ceil(log(N2, 3)) + 1):
        current3 = 3**i3
        if current3 > N2:
            choice.append([3] * i3)
            break
        for i5 in range(ceil(log(N2, 5)) + 1):
            current5 = current3 * 5**i5
            if current5 > N2:
                choice.append([3] * i3 + [5] * i5)
                break
            for i7 in range(ceil(log(N2, 7)) + 1):
                current7 = current5 * 7**i7
                if current7 > N2:
                    choice.append([3] * i3 + [5] * i5 + [7] * i7)
                    break
                for i9 in range(ceil(log(N2, 9)) + 1):
                    current9 = current7 * 9**i9
                    if current9 > N2:
                        choice.append([3] * i3 + [5] * i5 + [7] * i7 +
                                      [9] * i9)
                        break
                    for i11 in range(ceil(log(N2, 11)) + 1):
                        current11 = current9 * 11**i11
                        if current11 > N2:
                            choice.append([3] * i3 + [5] * i5 + [7] * i7 +
                                          [9] * i9 + [11] * i11)
                            break

    # print(len(choice))
    # min_n = 10 ** 20  # inf
    # min_comp = []
    # for seq in choice:
    #     current_n = product(i**(j//2) for i, j in zip(primes, reversed(seq)))
    #     # print(current_n, seq)
    #     if current_n < min_n:
    #         min_n = current_n
    #         min_comp = seq
    # print(min_comp)

    min_n = min(
        product(i**(j // 2) for i, j in zip(primes, reversed(seq)))
        for seq in choice)
    print(min_n)
예제 #8
0
def main():
    ceiling = 5 * 10 ** 7
    primes = prime_under(int((ceiling - 2**3 - 2**4) ** 0.5) + 1)
    nums = set()
    print("Primes generated by:", time() - starting_time, "seconds")

    for i in primes:
        for j in primes:
            for k in primes:
                num = i**4 + j**3 + k**2
                if num > ceiling:
                    break
                nums.add(num)
    print(len(nums))
예제 #9
0
def main():
    N = 10**5

    # heuristic and slow!
    print("\nheuristic and slow!")
    D = 50  # probe depth
    primes = prime_under(N)
    ever = []
    for p in primes[3:]:
        if any(repunit_mod(p, i) == 0 for i in range(1, D + 1)):
            # print(p)
            ever.append(p)
            continue
    print(ever)
    print(sum(primes) - sum(ever))
예제 #10
0
def main():
    N = 10 ** 5

    # heuristic and slow!
    print("\nheuristic and slow!")
    D = 50  # probe depth
    primes = prime_under(N)
    ever = []
    for p in primes[3:]:
        if any(repunit_mod(p, i) == 0 for i in range(1, D+1)):
            # print(p)
            ever.append(p)
            continue
    print(ever)
    print(sum(primes) - sum(ever))
예제 #11
0
def main():
    N = 4 * 10 ** 6
    N2 = N * 2 + 1
    primes = prime_under(50)
    # print(log(N2, 3), len(primes), list(enumerate(primes)), sep='\n')

    choice = []
    for i3 in range(ceil(log(N2, 3))+1):
        current3 = 3 ** i3
        if current3 > N2:
            choice.append([3]*i3)
            break
        for i5 in range(ceil(log(N2, 5))+1):
            current5 = current3 * 5 ** i5
            if current5 > N2:
                choice.append([3]*i3 + [5]*i5)
                break
            for i7 in range(ceil(log(N2, 7))+1):
                current7 = current5 * 7 ** i7
                if current7 > N2:
                    choice.append([3]*i3 + [5]*i5 + [7]*i7)
                    break
                for i9 in range(ceil(log(N2, 9))+1):
                    current9 = current7 * 9 ** i9
                    if current9 > N2:
                        choice.append([3]*i3 + [5]*i5 + [7]*i7 + [9]*i9)
                        break
                    for i11 in range(ceil(log(N2, 11))+1):
                        current11 = current9 * 11 ** i11
                        if current11 > N2:
                            choice.append([3]*i3 + [5]*i5 + [7]*i7 + [9]*i9 + [11]*i11)
                            break

    # print(len(choice))
    # min_n = 10 ** 20  # inf
    # min_comp = []
    # for seq in choice:
    #     current_n = product(i**(j//2) for i, j in zip(primes, reversed(seq)))
    #     # print(current_n, seq)
    #     if current_n < min_n:
    #         min_n = current_n
    #         min_comp = seq
    # print(min_comp)

    min_n = min(product(i**(j//2) for i, j in zip(primes, reversed(seq))) 
                for seq in choice)
    print(min_n)
예제 #12
0
def main():
    # print(m(2, 3, 1000))

    N = 10000000
    m_vals = []
    primes = prime_under(N // 2 + 1)
    primes_len = len(primes)
    print("Primes computed by", time() - starting_time)

    for i in range(primes_len):
        if primes[i] * primes[i] > N:
            break
        for j in range(i + 1, primes_len):
            if primes[i] * primes[j] > N:
                break
            m_curr = m(primes[i], primes[j], N)
            # print(primes[i], primes[j], m_curr)
            m_vals.append(m_curr)

    print(sum(m_vals))
예제 #13
0
def main():
    # print(m(2, 3, 1000))

    N = 10000000
    m_vals = []
    primes = prime_under(N // 2 + 1)
    primes_len = len(primes)
    print("Primes computed by", time() - starting_time)

    for i in range(primes_len):
        if primes[i] * primes[i] > N:
            break
        for j in range(i + 1, primes_len):
            if primes[i] * primes[j] > N:
                break
            m_curr = m(primes[i], primes[j], N)
            # print(primes[i], primes[j], m_curr)
            m_vals.append(m_curr)

    print(sum(m_vals))
예제 #14
0
def main():
    N = 10 ** 6
    primes = prime_under(N + int(N ** 0.5))
    print("Primes generated:", time() - starting_time)

    sigma_s = 0
    for i in range(3, len(primes)):  # p1 starting from 5, p2 starting from 7
        p1 = primes[i-1]
        p2 = primes[i]
        if p1 > N:
            break

        last_digits = 10 ** len(str(p1))
        a = last_digits % p2
        b = p2 - p1
        curr_s = linear_mod(a, b, p2) * last_digits + p1
        # print(p1, p2, curr_s)
        sigma_s += curr_s

    print(sigma_s)
예제 #15
0
def main():
    N = 2 ** 30
    sqrt_N = int(N ** 0.5) + 1
    primes = prime_under(sqrt_N)
    primes_len = len(primes)
    print(primes_len, "primes generated by", time() - starting_time)

    max_num_primes = 0
    prime_product = primes[0]
    while prime_product < sqrt_N:
        max_num_primes += 1
        prime_product *= primes[max_num_primes]
    print("max number of primes", max_num_primes)

    num_square_free = N

    def square_free(start_index, level, last):
        nonlocal num_square_free

        for index in range(start_index, primes_len):
            item = primes[index]
            new_last = last * item
            if new_last >= N:
                break
            num_square_free += N // (new_last * new_last) * (-1)**level
            if level < max_num_primes:
                square_free(index+1, level+1, new_last)

    square_free(0, 1, 1)

    # num_square_free = N
    # flag = 1
    # for num_primes in range(1, max_num_primes+1):
    #     print(num_primes, num_square_free, time() - starting_time)
    #     flag *= -1
    #     # print(generate_prime_tuple(primes, 0, num_primes, (), lambda l, a: product(l) * a < N))
    #     for prime_prod in generate_prime_prod_apply(primes, 0, num_primes, 1, lambda l, a: l*a < N, lambda a, b: a*b):
    #         squared = prime_prod ** 2
    #         num_square_free += N // squared * flag

    print(num_square_free)
예제 #16
0
def main():
    N = 2**30
    sqrt_N = int(N**0.5) + 1
    primes = prime_under(sqrt_N)
    primes_len = len(primes)
    print(primes_len, "primes generated by", time() - starting_time)

    max_num_primes = 0
    prime_product = primes[0]
    while prime_product < sqrt_N:
        max_num_primes += 1
        prime_product *= primes[max_num_primes]
    print("max number of primes", max_num_primes)

    num_square_free = N

    def square_free(start_index, level, last):
        nonlocal num_square_free

        for index in range(start_index, primes_len):
            item = primes[index]
            new_last = last * item
            if new_last >= N:
                break
            num_square_free += N // (new_last * new_last) * (-1)**level
            if level < max_num_primes:
                square_free(index + 1, level + 1, new_last)

    square_free(0, 1, 1)

    # num_square_free = N
    # flag = 1
    # for num_primes in range(1, max_num_primes+1):
    #     print(num_primes, num_square_free, time() - starting_time)
    #     flag *= -1
    #     # print(generate_prime_tuple(primes, 0, num_primes, (), lambda l, a: product(l) * a < N))
    #     for prime_prod in generate_prime_prod_apply(primes, 0, num_primes, 1, lambda l, a: l*a < N, lambda a, b: a*b):
    #         squared = prime_prod ** 2
    #         num_square_free += N // squared * flag

    print(num_square_free)
예제 #17
0
def main():
    primes = prime_under(10 ** 7)
    # print(len(primes), time() - starting_time)
    factors = [2]
    frontier = [4]
    heapq.heapify(frontier)

    prime_index = 1  # 2 already included
    for i in range(500500 - 1):
        if frontier[0] >= primes[prime_index]:
            # push square of the current prime into the queue
            heapq.heappush(frontier, primes[prime_index] ** 2)
            factors.append(primes[prime_index])
            prime_index += 1
        else:
            minimum = heapq.heappop(frontier)
            heapq.heappush(frontier, minimum ** 2)
            factors.append(minimum)

    # print(factors)
    print(product_mod(factors, 500500507))
예제 #18
0
def main():
    N = 120000

    primes = prime_under(N)
    primes_len = len(primes)
    pf_cache = [[0], [1]]
    pf_cache.extend(prime_factors_under(N))
    print("preprocessing finished", time() - starting_time, "seconds")

    abc_hit = []

    # 2: a = 1
    for i in range(2, len(pf_cache)):  # exclude 0, 1
        if len(pf_cache[i]) == 1 and len(
                pf_cache[i -
                         1]) == 1 and pf_cache[i][0] * pf_cache[i - 1][0] < i:
            abc_hit.append(i)

    # 3
    print(len(list(rads3(N, primes, primes_len))))
    for prod, ps in rads3(N, primes, primes_len):
        pass

    # 4
    print(len(list(rads4(N, primes, primes_len))))

    # 5
    print(len(list(rads5(N, primes, primes_len))))

    # 6
    print(len(list(rads6(N, primes, primes_len))))

    # for a, c in prod_cache:
    #     if prod_cache[a] * prod_cache[c-a] * prod_cache[c] < c:
    #         abc_hit.append(c)
    #         # print(a, c-a, c, pf_cache[c])
    print(len(abc_hit))
    print(sum(abc_hit))
    print(abc_hit)
예제 #19
0
def main():
    squbes200 = []
    primes = prime_under(int(MAX ** 0.5))
    prime_len = len(primes)

    for i in range(prime_len):
        if primes[i] ** 3 > MAX:
            break
        for j in range(i+1, prime_len):
            sqube = primes[i] ** 3 * primes[j] ** 2
            if sqube > MAX:
                break
            if '200' in str(sqube):
                squbes200.append(sqube)
            sqube = primes[i] ** 2 * primes[j] ** 3
            if '200' in str(sqube) and sqube < MAX:
                squbes200.append(sqube)

    squbes200 = sorted(filter(lambda a: a < MAX, squbes200))
    # print(squbes200[-1])
    # print(len(squbes200))

    print(list(islice((s for s in squbes200 if prime_proof(s)), 200))[-1])
예제 #20
0
def main():
    squbes200 = []
    primes = prime_under(int(MAX**0.5))
    prime_len = len(primes)

    for i in range(prime_len):
        if primes[i]**3 > MAX:
            break
        for j in range(i + 1, prime_len):
            sqube = primes[i]**3 * primes[j]**2
            if sqube > MAX:
                break
            if '200' in str(sqube):
                squbes200.append(sqube)
            sqube = primes[i]**2 * primes[j]**3
            if '200' in str(sqube) and sqube < MAX:
                squbes200.append(sqube)

    squbes200 = sorted(filter(lambda a: a < MAX, squbes200))
    # print(squbes200[-1])
    # print(len(squbes200))

    print(list(islice((s for s in squbes200 if prime_proof(s)), 200))[-1])
예제 #21
0
def main():
    N = 120000

    primes = prime_under(N)
    primes_len = len(primes)
    pf_cache = [[0], [1]]
    pf_cache.extend(prime_factors_under(N))
    print("preprocessing finished", time() - starting_time, "seconds")

    abc_hit = []

    # 2: a = 1
    for i in range(2, len(pf_cache)):  # exclude 0, 1
        if len(pf_cache[i]) == 1 and len(pf_cache[i-1]) == 1 and pf_cache[i][0] * pf_cache[i-1][0] < i:
            abc_hit.append(i)

    # 3
    print(len(list(rads3(N, primes, primes_len))))
    for prod, ps in rads3(N, primes, primes_len):
        pass

    # 4
    print(len(list(rads4(N, primes, primes_len))))

    # 5
    print(len(list(rads5(N, primes, primes_len))))

    # 6
    print(len(list(rads6(N, primes, primes_len))))

    # for a, c in prod_cache:
    #     if prod_cache[a] * prod_cache[c-a] * prod_cache[c] < c:
    #         abc_hit.append(c)
    #         # print(a, c-a, c, pf_cache[c])
    print(len(abc_hit))
    print(sum(abc_hit))
    print(abc_hit)
예제 #22
0
"""

from mathutil import is_prime, prime_under


def is_truncatable(x):
    s = str(x)
    for i in range(1, len(s)):
        if int(s[:i]) not in primes:
            return False
    for i in range(-1, -len(s), -1):
        if int(s[i:]) not in primes:
            return False
    return True

primes = set(prime_under(800000))


def main():
    truncatable = []
    for prime in primes:
        if is_truncatable(prime):
            truncatable.append(prime)
            if len(truncatable) == 11:
                break

    print(truncatable)
    print(len(truncatable))
    print(sum(truncatable))

예제 #23
0
from mathutil import is_prime, prime_under


def is_truncatable(x):
    s = str(x)
    for i in range(1, len(s)):
        if int(s[:i]) not in primes:
            return False
    for i in range(-1, -len(s), -1):
        if int(s[i:]) not in primes:
            return False
    return True


primes = set(prime_under(800000))


def main():
    truncatable = []
    for prime in primes:
        if is_truncatable(prime):
            truncatable.append(prime)
            if len(truncatable) == 11:
                break

    print(truncatable)
    print(len(truncatable))
    print(sum(truncatable))