Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # wim's slicing idea from stackoverflow.com
- # using numpy arrays - less memory
- # JIT compiler jit nopeuttaa kun n > miljoona
- # Juhani Kaukoranta 24.3.2019
- import time
- import numpy as np
- from numba import jit
- @jit
- def primes(n):
- r = np.ones(n,dtype=bool)
- r[0] = r[1] = False
- r[4::2] = False
- for i in range(3,int(1 + np.sqrt(n)+1),2):
- if r[i]:
- r[i*i::2*i] = False
- return (np.flatnonzero(r))
- high = int(input("Anna kokonaisluku, jota pienemmät alkuluvut lasketaan "))
- time0 = time.perf_counter()
- alkuluvut = primes(high)
- time1 = time.perf_counter()
- print("Laskenta-aika = ",time1-time0," sekuntia")
- print("alkuluvut = ",alkuluvut)
- print("alkulukuja oli ",len(alkuluvut)," kpl")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement