Is_prime using WHILE


#1

Working on the is_prime excercise, and can't seem to get the hang of this one. I looked around and everyone else's examples use a FOR loop. If that's the only way to do it, then I'll re-write, but I started this with a WHILE loop, and I'd rather understand than just copy.. :slight_smile:

I am getting "Oops, try again. Your function fails on is_prime(5). It returns False when it should return True."

Can anyone help me out, or at least point me in the right direction?

def is_prime(x):
    if x < 2:
        return False
    else:
        if x == 2 or x == 3:
            return True
    y = x - 1
    p = 1
    while x > 0: 
        p = x / y
        if p - int(p) == 0:
            return False
            break
        else:
            y = y - 1
    else:
        return True

#2

Added some notes to help make sense of what I am trying to do.
def is_prime(x):
# This is to avoid weird case with 1.
if x < 2:
return False
# Added this because 2 is prime.
else:
if x == 2:
return True
# define the variables
y = x - 1
# using WHILE to step back through all numbers to zero.
while x > 1:
p = x / y
# does x divided by a smaller number give an integer?
if p - int(p) == 0:
return False
break
else:
y = y - 1
else:
return True


#3

In your code you are modifying y with the statement: y=y-1, but your while loop is dependent on x. You need to switch the while look to be for y and not x because x is not being modified every loop. Hope this helps :blush:


#4

So I changed the WHILE statement to use y instead of x (because we will repeatedly divide a not-changing x by a decreasing y). I now get the same error at 3 instead of 5.

y gets decremented in each iteration, so will either divide evenly and return False, or eventually will hit 1 and return True

Thoughts?

def is_prime(x):
    # This is to avoid weird case with 1.
    if x < 2:
        return False
    # Added this because 2 is prime.
    else:
        if x == 2:
            return True
    # define the variables
    y = x - 1
    p = 1
    # using a while loop to step back through all numbers to zero.
    while y > 1: 
        p = x / y
        # does x divided by a smaller number give an integer?
        if p - int(p) == 0:
            return False
            break
        else:
            # assuming that x doesnt divide into y evenly, 
            # decrement y and keep trying.
            y = y - 1
    else:
        return True

#5

OK - so I had a friend try this in a regular python IDE, and run the numbers 1-25 through it, and it gives all the right answers. So I'm guessing its more a problem with the parser than the script I wrote?

I changed the "if x == 2:" line to "if x == 2 or x == 3:" (at this point I just want to move on to the next lesson), and it went from failing on 3 to failing on 5. I'm guessing the parser is somehow incompatible with using a WHILE statement instead of a FOR loop with a range...


#6

so when I did this, I did two things differently.. I used a simple Modulo statement:

    while y > 1: 
        if x%y == 0:
            return False

I also had mine setup like

if x<2:
return False
elif x==2 or x==3:
return True
else:
(Here we build in our actual test of Prime numbers). Try using a simple Modulo instead of the clever p-int(p) solution. Should hopefully go through without a hook:

if all else fails
my code below that went through:
def is_prime(x):
if x < 2:
return False
elif x==2 or x==3:
return True
else:
y = x - 1
while y > 1:
if x%y == 0:
return False
break
else:
y = y - 1
return True


#7

if x < 2: return False
y = 2
while y < x:
    if x % y == 0: return False
    y += 1
return True

This is a brute force approach that covers more test values than it needs to, but that is what the lesson is based upon.


#9

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