def problem_0027(limit: 1000): max_a, max_b = 0, 0 maximum = 0 for a in range(-limit + 1, limit): for b in primes_until(limit): # b has to be prime, so we don't need the full range(-1000, 1001) amount = _quadratic_primes(a, b) if amount > maximum: maximum = amount max_a, max_b = a, b return max_a * max_b
def problem_0049() -> int: candidates = [prime for prime in primes_until(10000) if prime > 1000] map = defaultdict(list) for candidate in candidates: map[''.join(sorted(str(candidate)))].append(candidate) for grouping in map.values(): for c in combinations(grouping, 3): if c == (1487, 4817, 8147): continue if c[1] - c[0] == c[2] - c[1]: return c[0] * 10**8 + c[1] * 10**4 + c[2]
def _get_primes_of_length( digits: int) -> Tuple[List[int], Callable[[int], bool]]: """ Return the primes with a given number of digits :param digits: the number of digits the primes should have :return: A list of primes, and a function for determining membership """ # Sorted list for iteration sorted_primes = [ prime for prime in primes_until(10**digits) if prime > 10**(digits - 1) ] # Set for faster containment checking (faster even than binary search) primes_set = set(sorted_primes) return sorted_primes, lambda x: x in primes_set
def problem_0050(maximum: int) -> int: primes_list = list(primes_until(maximum)) primes_set = set(primes_list) maximum_consecutive = 0 p = 0 for i in range(len(primes_list)): total = 0 for j in range(i, len(primes_list)): total += primes_list[j] if total > maximum: break if total in primes_set and j - i > maximum_consecutive: maximum_consecutive = j - i p = total return p
def problem_0035(maximum: int) -> int: candidates = set(primes_until(maximum)) return sum(1 for candidate in candidates if all(rotation in candidates for rotation in _rotations(candidate)))
def problem_0037(): primes = set(primes_until(1000000)) is_prime = is_in(primes) return sum(prime for prime in primes if _is_truncatable(prime, is_prime))
from itertools import combinations from typing import Tuple, List, Dict, Set from projecteuler.util.timing import print_time from util.primes import primes_until, is_prime PRIMES = list(primes_until(10000)) # TODO remove list, if only iterated once def _is_prime_pair(prime1: int, prime2: int) -> bool: if not is_prime(int(str(prime1) + str(prime2))): return False if not is_prime(int(str(prime2) + str(prime1))): return False return True def problem_0060(set_size: int) -> int: cliques: Set[frozenset] = set() for primes in combinations(PRIMES, 2): if _is_prime_pair(*primes): cliques.add(frozenset(primes)) print(2, cliques) for size in range(3, set_size + 1): new_cliques: Set[frozenset] = set() for clique1, clique2 in combinations(cliques, 2): c = clique1.symmetric_difference(clique2) if len(c) == 2 and _is_prime_pair(*c):
def problem_0010(maximum: int) -> int: return sum(primes_until(maximum))