fork download
  1. import itertools
  2. import math
  3.  
  4.  
  5. def is_prime(n):
  6. assert n >= 1, f"n must be >= 1, is {n}"
  7. if n == 1:
  8. return False
  9. if n % 2 == 0:
  10. return True
  11.  
  12. for i in range(3, math.floor(math.sqrt(n))+1, 2):
  13. if n % i == 0:
  14. return False
  15.  
  16. return True
  17.  
  18.  
  19. def next_prime_after(n):
  20. next_odd = (n + 1) | 1
  21.  
  22. while not is_prime(next_odd):
  23. next_odd += 2
  24.  
  25. return next_odd
  26.  
  27.  
  28. def primes_from(n=2):
  29. if is_prime(n):
  30. yield n
  31.  
  32. while True:
  33. n = next_prime_after(n)
  34. yield n
  35.  
  36.  
  37. first_20_primes = itertools.islice(primes_from(2), 20)
  38. print(list(first_20_primes))
  39.  
Success #stdin #stdout 0.03s 9788KB
stdin
Standard input is empty
stdout
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71]