input: positive integer 'N' > 2 returns a list of prime numbers from 2 up to N. This function implements the algorithm called sieve of erathostenes.
(N)
| 91 | |
| 92 | |
| 93 | def sieveEr(N): |
| 94 | """ |
| 95 | input: positive integer 'N' > 2 |
| 96 | returns a list of prime numbers from 2 up to N. |
| 97 | |
| 98 | This function implements the algorithm called |
| 99 | sieve of erathostenes. |
| 100 | |
| 101 | """ |
| 102 | from math import sqrt |
| 103 | |
| 104 | # precondition |
| 105 | assert isinstance(N, int) and (N > 2), "'N' must been an int and > 2" |
| 106 | |
| 107 | primes = [True for x in range(N + 1)] |
| 108 | |
| 109 | for p in range(2, int(sqrt(N)) + 1): |
| 110 | if primes[p]: |
| 111 | for i in range(p * p, N + 1, p): |
| 112 | primes[i] = False |
| 113 | primes[0] = False |
| 114 | primes[1] = False |
| 115 | ret = [] |
| 116 | for p in range(N + 1): |
| 117 | if primes[p]: |
| 118 | ret.append(p) |
| 119 | |
| 120 | return ret |
| 121 | |
| 122 | |
| 123 | # -------------------------------- |