Advertisement
OgreVorbis

Primes Python

Dec 13th, 2021
1,366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. from math import sqrt
  2. import time
  3. nums = []
  4. s = 0
  5. n = 0
  6. m = 0
  7. lim = 0
  8. yorn = ''
  9.  
  10. print("Enter a limit for this search: ", end="")
  11. lim = int(input())
  12. if lim == 0:
  13.   lim = 100000000
  14. s = int(sqrt(lim))
  15. tStart = time.time()
  16.  
  17. nums = [0 for i in range(lim + 1)]
  18.  
  19. for n in range(2, s):
  20.   if nums[n] == False:
  21.     m = n * n
  22.     while m <= lim:
  23.       nums[m] = True
  24.       m += n
  25.  
  26. tStop = time.time()
  27. Elapsed = tStop - tStart
  28. print("It took ", (Elapsed % 60), " seconds to complete.")
  29. print("Do you want to list results (Y/N)? ", end="")
  30. yorn = input()
  31.  
  32. m = 0
  33. for n in range(2, lim):
  34.   if nums[n] == False:
  35.     if yorn == 'y':
  36.       print(n)
  37.     m += 1
  38. print("")
  39. print("Total primes = ", m)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement