0

Hi everyone, I'm new to Daniweb and programming in general. I wrote a program to implement Euler's sieve, and it seems to work for small primes. But, it incorrectly lists certain numbers as primes for larger ranges. For example, for n = 400, it lists 391 as prime. I've gone through the program again, but I can't see what's wrong with it. All help will be appreciated, thanks! Here's the code:

Python 2.7

n = int(raw_input("Num: "))
s = range(3, n+1, 2)
i = 1
l = 0 
a = len(s) - 1

while l < a: 
	if s[l] != 0: 
		j = (l + (s[l]*i)) 
		while (j <= a): 
			s[j] = 0 
			i += 1 
			j = (l + (s[l]*i))
	else: 
		if l < a: 
			while s[l] == 0 and l < a:
				l += 1
		if l == a: 
			break
	i = 1
	l += 1

s = list(set(s))
s.sort()
s.remove(0)
s = [2] + s
print s

Edited by Thisisnotanid: Added details.

1
Contributor
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Thisisnotanid
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.