Is_prime


#1
 def is_prime(x):

    if x>1:
        if x==2:
             return True
        else:
            for n in range(2,x):              
                    if x%n==0:
                        return False
                        break
                    else:
                        return True                    
    else:
        return False

#2

I dont know what am i doing wrong


#3

a function ends the moment a return keyword is reached (even when return keyword is reached in loop)

so, for x=9 the first case tested by the loop is n=2:

if 9 % 2 == 0

which evaluate to false. so else runs, True is returned, the function ends

What is the condition for a number to be a prime number? x shouldn’t be divislbe by any number in range, this is clearly not happening at the moment


Prime numbers
#4

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