6/15 is_prime code doesn't works


#1

Hi to all,

somebody can help me with this code, I'm going crazy and I'm not able to see where is the problem....

def is_prime(x):
    if x < 2:
        print 'is not prime'
        return False
    elif x == 2 or x == 3:
        print 'is prime'
        return True
    else:    
        for n in range(2, x):
            print x, n, range(2, x)
            if x % n == 0:
                print 'is prime'
                return True   
            else:
                print 'is not prime'
                return False
            
        
is_prime(7)

Thanks in advance...

David.


#2

Try with else: indent on the same level as for


#3

Thanks @dawidrz87, I've looked for in google and I found the correct answer....

def is_prime(x):
    if x < 2:
        print 'is not prime'
        return False
    elif x == 2 or x == 3:
        print 'is prime'
        return True
    else:    
        for n in range(2, x):
            print x, n, range(2, x)
            if x % n == 0:
                print 'is not prime'
                return False   
        return True
            
        
is_prime(9)

Now is working fine!!! But If I try to apply your solution, it works too....

Thanks.

David.


#4

I don't get it either. My code looks as follows:

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

However it says my code won't work for input (3).

What I would expect is:
function checks if (3) < 2 - No.
function checks if (3) > 2 -Yes.
Enter for loop. Range consists only of one number: 2 (between 2 and 2 there is no else).
if condition: if 3 % 2 == 0 return is set to false. Not the case, so:
else: return = true

But it won't work. Why?


#5

Check these posts out

Post 1

post 2

Both of these posts have very well though out explanations from not only myself but @mtf as well.

@megarunner02353 as to why your function does not work, do the following

print(range(2, 3))

After doing this you should see where your issue lies at.


#6

Ok, I got it working now.

But there are a few things I'd like to know if I'm right on this or not.
First the working code (brute force, no fancy maths):

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

Now my observation:
If a return condition is set, the function ends. The for loop stops that very moment.
Correct or incorrect?

Another question:

Why can I not put the return = True in a else condition, like so:

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

or at least inside the for loop, like so:

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

Sorry, solved. I get it now.
As soon as the first time True is returned, the loop ends.

Thanks @zeziba!


#7

Correct, the two things that break a loop are return and break

Also, a raise statement would break a loop too.