Lost almost in prime number


#1



not working


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


#2

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


#3

it worked well for me


#4

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


#5

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:


#6

yep, many things you can do to improve it:


#7

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