def totients(n): ''' Generates the phi array of Euler's totient function up to n, using sieving method. ''' prime = primes.prime_list(n) phi = [float(i) for i in range(n + 1)] for p in range(2, n + 1): if prime[p]: j = 1 while j * p <= n: phi[p * j] *= 1.0 - 1.0 / p j += 1 return list(map(round, phi))
''' Project Euler #7 Find the 10001st Prime ''' from primes import prime_list primes = prime_list(500000) prime_count = 1 x = 2 while x < 100001: x += 1 if primes[x]: prime_count += 1 print x
import math from primes import is_prime_c as is_prime from primes import prime_list l = 8500 primes_list = [3] + prime_list(l)[3:] len_primes_list = len(primes_list) def pair_prime(a, b): return is_prime(int(a + b)) and is_prime(int(b + a)) def f(): M = l * 5 for c1 in xrange(len_primes_list - 4): p1 = primes_list[c1] s1 = str(p1) for c2 in xrange(c1, len_primes_list - 3): p2 = primes_list[c2] t2 = p1 + p2 s2 = str(p2) if pair_prime(s1, s2): for c3 in xrange(c2, len_primes_list - 2): p3 = primes_list[c3] t3 = t2 + p3 s3 = str(p3) if pair_prime(s1, s3) and pair_prime(s2, s3): for c4 in xrange(c3, len_primes_list - 1): p4 = primes_list[c4] t4 = t3 + p4 if (t4 + p4) >= M:
import primes, time start = time.time() x = primes.prime_list(2000000) total = 0 for i in xrange(1, 2000000): if x[i]: total += i print total, time.time() - start