SHARE
TWEET

Untitled

a guest Jan 21st, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from math import sqrt
  2. import time
  3.  
  4. def prime(n):
  5.     if n<2: return False
  6.     if n==2: return True
  7.     return all(n%i for i in [2] + range(3, int(sqrt(n))+1, 2))
  8.  
  9. # return all primes up to n, fast as hell
  10. def get_primes(n):
  11.     numbers = range(3, n+1, 2)
  12.     half = n//2
  13.     current = 4
  14.  
  15.     for step in xrange(3, n+1, 2):
  16.         for i in xrange(current, half, step):
  17.             numbers[i-1]=0
  18.         current += 2*(step+1)
  19.  
  20.         if current > half:
  21.             p = [2] + filter(None, numbers)
  22.             if not prime(n):
  23.                 try: p.remove(n)
  24.                 except ValueError: pass
  25.             return p
  26.  
  27. start_time = time.time()
  28. print(sum(get_primes(2000000)))
  29. print("--- %s seconds ---" % (time.time() - start_time))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top