Why does is_prime fail for some numbers?


#50
if x < 2: return False

Then jump straight into the for loop.

I had no idea this would be meaningless to you. We need to back this thing up a few stations.


#51

Yes, I will relook over the exercise. I sought the solution, but then deleted thinking I could do it over, but now I can’t see the solution again for reference, and there’s no way to test for the answer. Is there a way to do that on the exercise itself?


#52

Here’s a spoiler that is a brute force approach similar to what the exercise suggests…

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

#53
def is_prime(x):
  if x < 2:
    return False
  
  for n in range(2, x):
    if x % n == 0:
      return False
    
    else:
      return True
    
print is_prime(25)

Can anyone help explain why this code is returning True for 25?


#54

Because you are returning True from inside the loop.


#55

Looks like I found one that works, but it wasn’t the same as the solution. I can’t get the solution to show anymore. :confused:

Can I report a bug also? I often get indentation errors that I can fix just by backspacing and then indenting (tabbing) or hitting enter again.

Eg.

If x == a_thing:
  print a_thing

Gives ‘indentation error’ until I backspace to previous indent, like so

If x == a_thing:
print a_thing

So then I either tab again, or backspace to previous line and then enter new line

If x == a_thing:
  print a_thing

This is a simple example, but it seems to do it on code with more lines in them.


#56
def is_prime(x):
    for n in (2, x-1):
      if x % n == 0:
        print str(x)+" is not prime"
        return False
      else:
        print str(x)+" is prime"
        return True
  

It isn’t able to read numbers 25 and above. Dunno why?


#57

Lots of code and not much reason. Sorry to say. What do you understand of Prime numbers? I mean this literally, so be explicit.


#58

O sorry, I meant to say, it thinks that 25 is prime

If you want to know - I figured out why x needs to be (x-1) in the loop, because otherwise it would return false due to hitting 0 on final iteration


#59

The top of the range can be x; that’s not a problem. What is a problem is having an else branch on the if statement. As a result your code will return True for all odd numbers.


#60

I have else on for, but it still returns true for odd. There’s nowhere else can go besides for and if on the code, so I dunno


#61

In your code above, the else is on the if, not the for, or at least that what it appears as.

We should not use else on a for loop unless there is a break in the for block. Just remove all the code after,

if x % n == 0: return False

and add a return True line at the end of the function. Check my earlier spoiler to see what I mean.


#62

Indentation error

def is_prime(x):
    for n in (2, x-1):
      if x % n == 0:
        print str(x)+" is not prime"
        return False
      else:
        print str(x)+" is prime"
  return True
      
is_prime(12)
is_prime(15)


#63

The return line should have the same indentation as the for line. Don’t print anything in your function. Just return False, or True. That else branch should be removed.

Remember to filter out all numbers less than 2.

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

#64
def is_prime(x):
  if x < 2:
    return False
  for n in (2, x-1):
    if x % n == 0:
      print str(x)+" is not prime"
      return False
  print str(x)+" is prime"
  return True
  
      
is_prime(12)
is_prime(15)

It still says that 15 is prime. Is the print statement messing with it?


#65

Missing keyword, range

for n in range(2, x):

Remove the print statements and print the return value at the caller.

print (is_prime(15))

#66

Woah, that’s weird I forgot range.

Anyway, it works now, regardless of print statements. Thanks for help


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

for the “return True” part, does it mean that if there is nothing returned after the inside loop, it will return True?


#69

Yes. If n makes it all the way to x then the loop terminates. That means x is not divisible by any n, which fits the definition of a Prime number, and why we return True.

if x % n == 0:
    return False

#70

I’ve read through all your answers in this thread, but I still don’t quite understand how the solution doesn’t permit x to enter the loop if it’s 2 and thus returns True.

When I added a print range(2, x) to my code and with x = 2, I actually got this list [2] in the console rather than the empty list you gave in your reply. So, from my understanding, shouldn’t it still be able to enter the loop?