Wout12345

Basic prime generator (Python 2)

Feb 15th, 2017
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.46 KB | None | 0 0
  1. primes = [2]
  2.  
  3. for value in xrange(3, 10000000, 2):
  4.  
  5. divisible = False
  6. for prime in primes:
  7. # Possible improvement: calculate floor(sqrt(value)) once outside this loop and check if prime <= floor(sqrt(value)) or something, sqrt is a much heavier function than **2 but would only have to be done once per value
  8. if prime**2 > value:
  9. break
  10. if value%prime == 0:
  11. divisible = True
  12. break
  13.  
  14. if not divisible:
  15. primes.append(value)
  16. print value
Advertisement
Add Comment
Please, Sign In to add comment