6/15 - is_prime


#1



def is_prime(x):
    if x < 2:
       return False
    elif x == 2:
       return True
    elif x == 3:
        return True
    else:
        for i in range(2, x):
            if x % i == 0:
                return False
        return True

Why the return True in the end can't be like:

  for i in range(2, x):
            if x % i == 0:
                return False
            else:
                return True


#2

Just because a number is not divisible by i as one value, does not mean it won't be divisible by another number in the range. We must complete iterating over the entire range before returning True.


#3

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