Is_prime


#1

i cant understand this please help.

def is_prime(x):
  for n in range(2 ,x-1):
    if x%n == 0 :
      return False
    else:
      return True

#2

what about numbers lesser then 2? They are not prime numbers and are not handled by the loop, make a if condition to check if x is lesser then 2, if so, return False

Now, by default at the end of a function, a function returns None, if we want to return something else at the end of the function, we can use the return keyword, but this will end the function

so your loop ends in the first iteration of your loop

but for a number to be a prime number, x shouldn’t be divisible by n in range 2 till x -1, so we only should return True after the loop


#6

A post was split to a new topic: Html


#7

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