def find_product_primes_below(N): prod = 1 l = [] for p in gen_sieve_eratosthenes(): prod *= p if prod >= N: return l l.append(p)
def solve(): result = 0 # @UnusedVariable primes = [p for p in gen_sieve_eratosthenes(10000)] primes_4digit = filter(lambda p: len(str(p)) == 4, primes) # prepare a map of permuted primes permuted_primes = {} for p in primes_4digit: try: permuted_primes[sorted_digits(p)] += [p] except KeyError, e: permuted_primes[sorted_digits(p)] = [p]
def solve(N): result = 0 # @UnusedVariable start = time.clock() sieve = {} for p in gen_sieve_eratosthenes(N + 1): sieve[p] = True print("Sieve prepared") circular_primes = [] for p in sorted(sieve.keys()): if is_circular_prime(p, sieve): circular_primes.append(p) print(circular_primes) result = len(circular_primes) # @UnusedVariable print("The number of circular primes below %(N)d is %(result)d" % vars()) print(time.clock() - start)
def primes_until(ceiling): primes = [p for p in gen_sieve_eratosthenes(ceiling)] if(primes[-1] > ceiling): return primes[0:-1] else: return primes
''' from sieve import gen_sieve_eratosthenes, is_prime def is_odd(n): return n%2 == 1 def calc_goldbach_numbers_dict(N, primes): goldbach_numbers = {} for p in primes: for n in range(1,N): gn = p + 2*(n**2) goldbach_numbers[gn] = True return goldbach_numbers def solve(N, primes): goldbach_nums = calc_goldbach_numbers_dict(N, primes) print('Prepared Goldbach numbers') L = min(N, primes[-1]) for i in range(9, L, 2): if not i in goldbach_nums and not i in primes: return i if __name__ == "__main__": primes = [p for p in gen_sieve_eratosthenes(10**4)] print('Prepared primes') result = solve(10**4, primes) print("The smallest odd composite that cannot be written as the sum of a prime and twice a square is %(result)d" % vars())
def find_ith_prime(i): primes = zip(range(0, i), gen_sieve_eratosthenes()) return primes[-1][1]
''' Find the first four consecutive integers to have four distinct primes factors. What is the first of these numbers? ''' from sieve import factorize_with_eratosthenes, gen_sieve_eratosthenes from projecteuler import boolean_product_of def has_k_primes(n, k, primes): l = len(set(factorize_with_eratosthenes(n, primes))) return l == k def solve(N, primes): n = 2 previous_factors_have_k_primes = [False] * N LIMIT = primes[-1]**2 for n in range(2, LIMIT): r = has_k_primes(n, N, primes) previous_factors_have_k_primes = previous_factors_have_k_primes[1:] + [r] if boolean_product_of(previous_factors_have_k_primes): return [n+i for i in range(-N+1, 1)] if __name__ == "__main__": consecutive_numbers = solve(4, [p for p in gen_sieve_eratosthenes(1000)]) print(consecutive_numbers) result = consecutive_numbers[0] print("Find the first four consecutive integers to have four distinct primes factors. The first of these numbers is %(result)d" % vars())