Is_prime


#1



it says that my code is failing. i cannot understand why.

def is_prime(x):
    if x<2:
        return False
    
    else:
        n=x-1
        while n>2:
            
            if x%n==0:
                return False 
                break
            else:
                n -=1
        else:
            return True


Replace this line with your code.


#2

Your function fails on is_prime(4). It returns True when it should return False.

This means there must be a problem with the loop conditional, n > 2. Try,

    while n >= 2:

We can further simplify your code...

def is_prime(x):
    if x < 2: return False    
    n = x - 1
    while n >= 2:
        if x % n == 0:
            return False 
            break
        else:
            n -= 1
    else:
        return True