def generate_primes(): n = 1 while True: while not t.is_prime(n): n += 1 yield n n += 1
def left_extend(prefix, curr): num = int(prefix + curr) if not t.is_prime(num): return if is_truncatable_prime(str(num)): truncatable_primes.append(num) for digit in extender_digits: left_extend(str(digit), prefix + curr)
def euler_totient(n): totient = n if t.is_prime(n): totient = n - 1 else: for prime in prime_divisors[n]: totient = totient // prime totient *= prime - 1 euler_totients.append((n, totient)) return (n, totient)
def add_all_pandigital_prime_sets(permutation): perm_partitions = get_all_partitions(permutation, 0) for partition in perm_partitions: candidate = frozenset(sorted(partition)) if candidate in unique_non_prime or candidate in unique: continue if all(t.is_prime(int(x)) for x in partition): unique.add(candidate) else: unique_non_prime.add(candidate)
def is_prime(x): if x in primes: return True elif x in nonprimes: return False elif t.is_prime(x): primes.append(x) return True else: nonprimes.append(x) return False
def get_prime_factorization_of_(n): prime_factors = [] if t.is_prime(n): primes.append(n) prime_factors.append(n) return prime_factors n_copy = n for prime in primes: while n_copy % prime == 0: prime_factors.append(prime) n_copy //= prime if n_copy == 1: return prime_factors
def is_prime(x): if x in primes: return True elif t.is_prime(x): return True return False
#!/usr/bin/env python3 -tt import naive_prime_test as t import numpy as np primes = [x for x in range(1, 1002) if t.is_prime(x)] def prime_divisors(n): return [prime for prime in primes if n % prime == 0] def euler_totient(n): totient = n for prime in prime_divisors(n): totient = totient // prime totient *= prime - 1 return totient print( np.argmax(np.array([n / euler_totient(n) for n in range(1, 1000000)])) + 1)
def is_right_truncatably_prime(x): return all(t.is_prime(int(x[i:])) for i in range(len(x)))
def is_left_truncatably_prime(x): return all(t.is_prime(int(x[:len(x) - i])) for i in range(len(x)))
n_copy = n for prime in primes: while n_copy % prime == 0: prime_factors.append(prime) n_copy //= prime if n_copy == 1: return prime_factors all_prime_factors = [ get_prime_factorization_of_(n) for n in range(2, upper_bound) ] for i in range(upper_bound - 2): n = i + 2 if t.is_prime(n): factorization_book[n] = {(n, )} continue factorizations = set() for pf in set(all_prime_factors[i]): factors_without_pf = n // pf factorizations_wo_pf = factorization_book[factors_without_pf] for factorization_wo_pf in factorizations_wo_pf: lst = sorted([pf] + list(factorization_wo_pf)) factorizations.add(tuple(lst)) fact_wo_pf_as_list = list(factorization_wo_pf) for index in range(len(fact_wo_pf_as_list)): lst = fact_wo_pf_as_list[:] lst[index] *= pf factorizations.add(tuple(sorted(lst))) factorization_book[n] = factorizations
#!/usr/bin/env python3 -tt import naive_prime_test as t import numpy as np prime_divisors = {} def list_digits(x): return "".join(sorted(str(x))) def have_same_digits(x, y): return list_digits(x) == list_digits(y) and len(str(x)) == len(str(y)) primes = [x for x in range(1,10000000//3 + 1) if t.is_prime(x)] print("got primes") def get_prime_divisors(n): results = [] n_copy = n for prime in primes: if n % prime == 0: results.append(prime) n_copy = n_copy // prime results.extend(prime_divisors[n_copy]) prime_divisors[n] = results prime_divisors[1] = [] for i in range(2, 10000000): get_prime_divisors(i)
#!/usr/bin/env python3 -tt import math import naive_prime_test as t # Find the sum of all the primes below two million. print(sum([x for x in range(2000000) if t.is_prime(x)]))
#!/usr/bin/env python3 -tt import math import naive_prime_test as t first_five = [1, 3, 5, 7, 9] last_prime = first_five[-1] total_primes = len(list(filter(t.is_prime, first_five))) total_diags = len([1, 3, 5, 7, 9]) i = 2 while total_primes / total_diags > 0.10: i += 2 for _ in range(4): last_prime += i if t.is_prime(last_prime): total_primes += 1 total_diags += 4 print(i + 1)