Search code examples
pythonsieve-of-eratosthenes

Why does my Sieve of Eratosthenes code return numbers ending in five?



import math

def sieve (x):
    primelist = list(range(2,x))
    for i in range (2,math.isqrt(x)):
        for n in range (i, x//i+1):
            y = n*i
            if y in primelist:
                primelist.remove(y)
        return primelist

My attempt to program the Sieve of Eratosthenes is above. I initially created this code on PyDroid, during a car ride, and on there it works perfectly. I have transferred this code to Python on Windows 11, and somehow on there it returns both prime numbers and numbers ending in 5. Please could somebody spot any errors in this code?


Solution

  • Unindent the return statement to take it out of the loop.