Example #1
0
File: 060.py Project: AlexClowes/pe
def main():
    # Guess biggest prime we want to check
    N = 10**4
    primes = list(get_primes_up_to_n(N))

    # Build dict to know which pairs of primes work
    prime_checking_set = set(get_primes_up_to_n(N**2))
    prime_concat_dict = defaultdict(set)
    for p, q in combinations(primes, 2):
        if concat(p, q) in prime_checking_set and concat(
                q, p) in prime_checking_set:
            prime_concat_dict[p].add(q)
            prime_concat_dict[q].add(p)

    # Choose primes from dict and look for minimum sum
    min_sum = 4 * N
    for p1 in primes:
        i2 = prime_concat_dict[p1]
        for p2 in i2:
            i3 = i2 & prime_concat_dict[p2]
            for p3 in i3:
                i4 = i3 & prime_concat_dict[p3]
                for p4 in i4:
                    i5 = i4 & prime_concat_dict[p4]
                    for p5 in i5:
                        total = p1 + p2 + p3 + p4 + p5
                        if total < min_sum:
                            min_sum = total
    print(min_sum)
Example #2
0
File: 049.py Project: AlexClowes/pe
def main():
    primes = set(get_primes_up_to_n(10**4)) - set(get_primes_up_to_n(10**3))
    for p in primes:
        prime_perms = set(get_perms(p)) & primes
        primes = primes - prime_perms
        prime_perms = sorted(prime_perms)
        for i in range(len(prime_perms)):
            pi = prime_perms[i]
            for j in range(i + 1, len(prime_perms)):
                pj = prime_perms[j]
                if 2 * pj - pi in prime_perms:
                    concat = str(pi) + str(pj) + str(2 * pj - pi)
                    if concat != "148748178147":
                        print(concat)
                        return
Example #3
0
File: 037.py Project: AlexClowes/pe
def main():
    primes = set(get_primes_up_to_n(10**6))
    total = 0
    for p in primes:
        if p > 10 and all(t in primes for t in gen_truncations(p)):
            total += p
    print(total)
Example #4
0
File: 070.py Project: AlexClowes/pe
def main():
    primes = list(get_primes_up_to_n(10**7))
    primes_set = set(primes)
    min_ratio = 10**7
    for n in range(2, 10**7):
        if n in primes_set:
            continue
        et = n
        n_copy = n
        early_exit = False
        for p in primes:
            if p * p > n_copy:
                break
            if n_copy % p == 0:
                et *= p - 1
                et //= p
                if n / et > min_ratio:
                    early_exit = True
                    break
                while n_copy % p == 0:
                    n_copy //= p
        if early_exit:
            continue
        if n_copy > 1:
            et *= n_copy - 1
            et //= n_copy
        if n / et < min_ratio and sorted(str(et)) == sorted(str(n)):
            min_ratio = n / et
            best_n = n
    print(best_n)
Example #5
0
File: 077.py Project: AlexClowes/pe
def main():
    primes = [2]
    for n in range(2, 100):
        if n > primes[-1]:
            primes = list(get_primes_up_to_n(n))
        if recurse(n, primes) > 5000:
            break
    print(n)
Example #6
0
File: 035.py Project: AlexClowes/pe
def main():
    primes = set(get_primes_up_to_n(10**6))
    count = 2  # Count 2 and 5, excluded by checks below
    for n in range(1, 10**6):
        if any((d % 2 == 0 or d == 5) for d in map(int, str(n))):
            continue
        if all(c in primes for c in cycle_number_gen(n)):
            count += 1
    print(count)
Example #7
0
def main():
    upper_lim = 10**8
    primes = list(get_primes_up_to_n(upper_lim // 2))
    count = 0
    for i in range(len(primes)):
        for j in range(i, len(primes)):
            if primes[i] * primes[j] < upper_lim:
                count += 1
            else:
                break
    print(count)
Example #8
0
def main():
    N = 10**9
    primes = list(get_primes_up_to_n(100))

    hamming_nos = {1}
    for p in primes:
        for x in set(hamming_nos):
            prod = x * p
            while prod <= N:
                hamming_nos.add(prod)
                prod *= p
    print(len(hamming_nos))
Example #9
0
File: 347.py Project: AlexClowes/pe
def S(N):
    primes = list(get_primes_up_to_n(N // 2))
    total = 0
    for i in range(len(primes)):
        p = primes[i]
        if p * p > N:
            break
        for j in range(i + 1, len(primes)):
            q = primes[j]
            if p * q > N:
                break
            total += M(p, q, N)
    return total
Example #10
0
File: 133.py Project: AlexClowes/pe
def main():
    primes = get_primes_up_to_n(10**5)
    total = 10  # 2 + 3 + 5
    for p in primes:
        if p < 7:
            continue
        n = A(p)
        while n % 2 == 0:
            n //= 2
        while n % 5 == 0:
            n //= 5
        if n != 1:
            total += p
    print(total)
Example #11
0
def main():
    n_max = 4 * 10**7
    target_length = 25

    primes = list(get_primes_up_to_n(n_max))
    totients = totient_sieve(n_max, np.array(primes))

    @njit
    def chain_length(n):
        if n == 1:
            return 1
        return 1 + chain_length(totients[n])

    print(sum(p for p in primes if chain_length(p) == target_length))
Example #12
0
File: 500.py Project: AlexClowes/pe
def smallest_with_2n_divisors(n):
    primes = get_primes_up_to_n(10 ** 7)
    prod = 1
    max_prime = next(primes)
    heap = [max_prime]
    for _ in range(n):
        next_term = heapq.heappop(heap)
        heapq.heappush(heap, next_term * next_term)
        if next_term == max_prime:
            max_prime = next(primes)
            heapq.heappush(heap, max_prime)
        prod *= next_term
        prod %= MOD
    return prod
Example #13
0
File: 216.py Project: AlexClowes/pe
def count_primes(n_max):
    tn_is_prime = np.ones(n_max + 1, dtype=np.bool)
    primes = [
        p for p in get_primes_up_to_n(int(2**0.5 * n_max)) if p % 8 in (1, 7)
    ]
    for p in primes:
        if p % 8 == 7:
            # In this case, square root is easy to find
            r = powmod((p - 1) // 2, (p + 1) // 4, p)
            r = min(r, p - r)
        else:
            r = tonelli_shanks(p, (p + 1) // 2)
        tn_is_prime[p - r::p] = False
        tn_is_prime[p + r::p] = False
    return np.sum(tn_is_prime[2:])
Example #14
0
def main():
    N = 50 * 10**6
    primes = list(get_primes_up_to_n(ceil(sqrt(N))))
    prime_2 = [p**2 for p in primes]
    prime_3 = [p**3 for p in primes]
    prime_4 = [p**4 for p in primes]
    is_sum = [False] * N
    for a in prime_2:
        for b in prime_3:
            if a + b > N:
                break
            for c in prime_4:
                if a + b + c > N:
                    break
                is_sum[a + b + c - 1] = True
    print(sum(is_sum))
Example #15
0
File: 051.py Project: AlexClowes/pe
def main():
    primes = list(get_primes_up_to_n(10**6))
    done = False
    for p in primes:
        digits = str(p)
        unique_digits = set(digits)
        for u_digit in unique_digits:
            p_with_asterisks = [
                p_digit if p_digit != u_digit else "*" for p_digit in digits
            ]
            rep_family = list(
                filter(is_prime, get_replacement_family(p_with_asterisks)))
            if len(rep_family) == 8:
                print(min(rep_family))
                done = True
                break
        if done:
            break
Example #16
0
File: 050.py Project: AlexClowes/pe
def main():
    primes = list(get_primes_up_to_n(10**6))
    primes_set = set(primes)

    prime_sum = 0
    max_sum_len = 0
    while prime_sum < primes[-1]:
        prime_sum += primes[max_sum_len]
        max_sum_len += 1

    for sum_len in range(max_sum_len, 1, -1):
        prime_sum = sum(primes[:sum_len])
        pos = 0
        while pos < len(primes) - sum_len and prime_sum < primes[-1]:
            if prime_sum in primes_set:
                print(prime_sum)
                return
            prime_sum += primes[pos + sum_len] - primes[pos]
            pos += 1
Example #17
0
File: 072.py Project: AlexClowes/pe
def main():
    N = 10**6
    primes = list(get_primes_up_to_n(N))

    def euler_totient(n):
        ret = n
        for p in primes:
            if p * p > n:
                break
            if n % p == 0:
                ret *= (p - 1)
                ret //= p
                while n % p == 0:
                    n //= p
        if n > 1:
            ret *= (n - 1)
            ret //= n
        return ret

    print(sum(euler_totient(r) for r in range(1, N + 1)) - 1)
Example #18
0
def main():
    N = 12 * 10**4

    primes = list(get_primes_up_to_n(N))

    @lru_cache(maxsize=None)
    def rad(n):
        ret = 1
        for p in primes:
            if p > n:
                break
            if n % p == 0:
                ret *= p
                n //= p
        return ret

    # Possible optimisation - compute radicals and primes simultaneously
    rads = defaultdict(list)
    for n in range(1, N):
        rads[rad(n)].append(n)
    rads = sorted((rad, lst) for rad, lst in rads.items())

    total = 0
    for c in range(2, N):
        rad_c = rad(c)
        if 2 * rad_c >= c:
            continue
        for rad_a, a_vals in rads:
            if 2 * rad_a * rad_c >= c:
                break
            for a in a_vals:
                if 2 * a > c:
                    break
                if rad_a * rad(c - a) * rad_c < c and gcd(c - a, a) == 1:
                    total += c
    print(total)
Example #19
0
def main():
    N = 5 * 10**7
    primes = list(get_primes_up_to_n(N))
    print(sum((p % 4 == 3) + (p * 4 < N) + (p * 16 < N) for p in primes))
Example #20
0
File: 010.py Project: AlexClowes/pe
def main():
    print(sum(get_primes_up_to_n(2 * 10**6)))
Example #21
0
File: 146.py Project: AlexClowes/pe
def main():
    n_max = 15 * 10**7
    primes = np.fromiter(get_primes_up_to_n(n_max + 27), dtype=np.int32)[4:]
    print(sum(n for n in range(10, n_max, 10) if consec_primes(n, primes)))
Example #22
0
def main():
    primes = get_primes_up_to_n(10**6)
    for i, p in enumerate(primes, 1):
        if i % 2 == 1 and (2 * i * p) % (p * p) > 10**10:
            print(i)
            break
Example #23
0
File: 132.py Project: AlexClowes/pe
def main():
    k = 10 ** 9
    primes = get_primes_up_to_n(10 ** 6)
    prime_factors = (p for p in primes if p > 5 and pow(10, k % (p - 1), p) == 1)
    print(sum(islice(prime_factors, 40)))
Example #24
0
def main():
    primes = list(get_primes_up_to_n(10**6 + 3))
    print(sum(f(p1, p2) for p1, p2 in zip(primes[2:], primes[3:])))
Example #25
0
File: 231.py Project: AlexClowes/pe
def bin_sum_primes(n, r):
    primes = get_primes_up_to_n(n)
    return sum(p * bin_padic_val(n, r, p) for p in primes)
Example #26
0
def test_get_primes_up_to_n():
    assert list(utils.get_primes_up_to_n(10)) == [2, 3, 5, 7]
    assert list(utils.get_primes_up_to_n(4)) == [2, 3]
Example #27
0
File: 381.py Project: AlexClowes/pe
def main():
    primes = np.fromiter(get_primes_up_to_n(10**8), dtype=np.int32)[2:]
    print(np.sum(S(primes)))
Example #28
0
File: 211.py Project: AlexClowes/pe
def main():
    n_max = 64 * 10 ** 6
    primes = np.fromiter(get_primes_up_to_n(n_max), dtype=np.int32)
    print(sum_square_sigma(n_max, primes))