Lost almost in prime number


not working

Replace this line with your code. 
def is_prime(x):
    if x<2:
        return False
    if x==2:
        return True
        for n in range(2,x+1):
            if x%n==0:
                return False
                return True


Shouldn't it be like x-1 instead of x+1? Otherwise it will try x%x which is always 0


it worked well for me


that should resolve your issue? By doing +1 it won't work. because if x = 3 then you get the following condition:

if 3 % 3 == 0:

which is true, so false will be returned, while 3 is a prime number and should return true

so you don't want to include x, so change x+1 to x, range won't includes is stop value, so doing x-1 is a bit extreme


Ah, good to know regarding the range.

Just a thought: We could even make it more extreme than that, with a range from 3 to x/2 to reduce the number of cycles. I'm sure this could be improved even more. :grinning:


yep, many things you can do to improve it:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.