def q10(): pg = gen_primes() sum = 0 prime = next(pg) while prime < 2000000: sum += prime prime = next(pg) print("Primes < 2M, summed:", sum)
def summation_of_primes(limit): """ adds all the primes up to some limit limit (int): limit, exclusive returns (int): result """ stub = Stub() stub.start() result = 0 for prime in gen_primes(limit): result += prime return result
def q3(): """ Uses the trial division method in order to find the prime factors. """ num = 600851475143 pg = gen_primes() prime = next(pg) # keep track of all the factors factors = [] # no need to go larger than sqrt(num) while (prime < floor(sqrt(num))): if (num % prime == 0): factors.append(prime) prime = next(pg) return factors
import math import itertools from euler import is_prime, gen_primes ELEMENTS_NUM = 4 SEQUENCE_SIZE = 3 START_NUMBER = 10 ** (ELEMENTS_NUM-1) MAX_NUMBER = (10**ELEMENTS_NUM) MAX_INTERVAL = int(MAX_NUMBER/SEQUENCE_SIZE) possible_primes = (prime for prime in gen_primes(limit=MAX_NUMBER) if prime >= START_NUMBER) primes_seq = [] for sequence_first in possible_primes: for interval in xrange(2, MAX_INTERVAL, 2): sequence_second = sequence_first+interval if sequence_second > MAX_NUMBER or not is_prime(sequence_second): continue sequence_third = sequence_second+interval if sequence_third > MAX_NUMBER or not is_prime(sequence_third): continue list_1 = [d for d in str(sequence_first)] list_2 = [d for d in str(sequence_second)] list_3 = [d for d in str(sequence_third)] #- Sorting the digits list to compare them later, if the don't have the same order they aren't equal -# list_1.sort() list_2.sort() list_3.sort() #- Checking if the 3 primes with equal intervals are permutations of one another and have 4 elements -# if list_1 == list_2 == list_3: primes_seq.append((sequence_first, sequence_second, sequence_third))
def q7(): pg = gen_primes() for _ in range(10000): next(pg) print("10,001th prime:", next(pg))
import math import itertools from euler import gen_primes, is_prime A_LIMIT = 1000 B_LIMIT = 1000 product = (0, 0) max_n = 0 for a in xrange(-A_LIMIT+1, A_LIMIT): #- b has to be a primer number always -# for b in gen_primes(B_LIMIT): for n in itertools.count(): #- Absolute value of the quadratic formula result -# num = abs((n**2) + (a*n) + b) if not is_prime(num): break if n > max_n: max_n = n product = (a, b) a, b = product print "n = %d, a = %d, b = %d, a*b = %d" % (max_n, a, b, a*b)
import math import itertools from euler import gen_primes print sum(prime for prime in gen_primes(2*10**6))
import math import itertools from euler import is_prime, gen_primes def is_circular_prime(prime): digits = [c for c in str(prime)] for x in xrange(0,len(digits)): digits.append(digits.pop(0)) rotated_num = int(''.join(digits)) if not is_prime(rotated_num): return False return True print len([prime for prime in gen_primes(1000000) if is_circular_prime(prime)])
import math from euler import is_prime, gen_primes LIMIT = 10**6 primes_list = list(gen_primes(LIMIT)) primes_set = set() start_offset = 0 end_offset = 0 cons_primes = [] nums_sum = 0 largest_cons_primes = [] while True: try: current_prime = primes_list[end_offset] except IndexError: start_offset += 1 if start_offset > len(primes_list): break end_offset = start_offset cons_primes = [] nums_sum = 0 cons_primes.append(current_prime) nums_sum += current_prime if nums_sum > LIMIT: start_offset += 1 end_offset = start_offset cons_primes = [] nums_sum = 0 continue if is_prime(nums_sum) and len(cons_primes) > len(largest_cons_primes):