Skip to content

Commit c483a01

Browse files
authored
Updated sieve of erathostenes function
Previous Code was not working, so i changed xrange to range (so the code could work on both python2 and python3). In one of the For loop the upper bound of range was not an integer, so changed it to integer. And for index 0 and 1 primes list need to be set to False.
1 parent 7f7a169 commit c483a01

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

primelib/primelib.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,16 @@ def sieveEr(N):
100100
# precondition
101101
assert isinstance(N, int) and (N > 2), "'N' must been an int and > 2"
102102

103-
primes = [True for x in xrange(N + 1)]
103+
primes = [True for x in range(N + 1)]
104104

105-
for p in xrange(2, sqrt(N) + 1):
105+
for p in range(2, int(sqrt(N)) + 1):
106106
if (primes[p]):
107-
for i in xrange(p*p, N + 1, p):
107+
for i in range(p*p, N + 1, p):
108108
primes[i] = False
109-
109+
primes[0]=False
110+
primes[1]=False
110111
ret = []
111-
for p in xrange(N + 1):
112+
for p in range(N + 1):
112113
if primes[p]:
113114
ret.append(p)
114115

0 commit comments

Comments
 (0)