Advertisement
jukaukor

sieve_wim_numpy_jit.py

Mar 24th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. # wim's slicing idea from stackoverflow.com
  2. # using numpy arrays - less memory
  3. # JIT compiler jit nopeuttaa kun n > miljoona
  4. # Juhani Kaukoranta 24.3.2019
  5. import time
  6. import numpy as np
  7. from numba import jit
  8. @jit
  9. def primes(n):
  10. r = np.ones(n,dtype=bool)
  11. r[0] = r[1] = False
  12. r[4::2] = False
  13. for i in range(3,int(1 + np.sqrt(n)+1),2):
  14. if r[i]:
  15. r[i*i::2*i] = False
  16. return (np.flatnonzero(r))
  17. high = int(input("Anna kokonaisluku, jota pienemmät alkuluvut lasketaan "))
  18. time0 = time.perf_counter()
  19. alkuluvut = primes(high)
  20. time1 = time.perf_counter()
  21. print("Laskenta-aika = ",time1-time0," sekuntia")
  22. print("alkuluvut = ",alkuluvut)
  23. print("alkulukuja oli ",len(alkuluvut)," kpl")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement