fork download
  1. # your code goes here
  2. def get_primes(limit):
  3. """Returns a list of prime numbers up to `limit` using Sieve of Eratosthenes."""
  4. if limit < 2:
  5. return []
  6.  
  7. is_prime = [True] * (limit + 1)
  8. is_prime[0] = is_prime[1] = False
  9.  
  10. for p in range(2, int(limit**0.5) + 1):
  11. if is_prime[p]:
  12. for i in range(p * p, limit + 1, p):
  13. is_prime[i] = False
  14.  
  15. return [num for num, prime in enumerate(is_prime) if prime]
  16.  
  17.  
  18. # Print primes in range [1, 100]
  19. start, end = 1, 100
  20. primes = [p for p in get_primes(end) if p >= start]
  21.  
  22. for prime in primes:
  23. print(prime)
Success #stdin #stdout 0.08s 14092KB
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
73
79
83
89
97