 # Why does is_prime fail for some numbers?

No you didn’t

Ok, strange, I can’t reproduce it anymore, so I must have been mistaken while re-running my code many times over. Thanks for pointing it out.

Hey guys. Just finished this exercise and I really want to thank people for going into such detail about it. When I first attacked the problem, I tried to begin like this (in pseudo code):
If x divided by n does not have a remainder then return true. But you guys went into detail about how logically the function can’t prove that the number can be prime from within the loop - only by brute forcing the failures do we learn that the number is prime.

Then when I made the second mistake - placing an else statement from within the loop to return True. Which of course caused the function to stop as soon as it checked the first condition…

Examples like this help me understand the logic of programming… a lot of times I feel like I’m going through the motions but honestly diving deep into this really helped me! Thanks! @mtf

3 Likes

Why is ‘else’ not working here? I tried an else after the if to accommodate for the 2, but it doesn’t work.

Sorry, my bad. I placed it wrongly.

Do we even need an `else` at all?

I just assumed that else is needed to accommodate all possible conditions. So, I was surprised when it worked without else. Why is else not necessary?

1 Like

Because we can handle both `2` and other primes that make it through the exclusion loop.

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

When x is 2, it will be rejected by the loop since range (2, x) will be (2, 1) and the loop will not run. 2 falls through as will any other value for `x` that is not divisible by any `n`.

Thank you for explaining that to me. So, code basically runs sequentially, but lines don’t forget the code that comes before them? Like I can say “return a” “if this condition is met”, and in subsequent lines, if I say “return a” with no condition attached, it remembers not to return the a which it returned before for the condition?

Code is sequential yes. It runs from the top down, one statement at a time.

Above, the first statement will exit the function with a return value of `False` if we supply an input that is less than 2. That line never runs again.

Failing the first line (meaning x is 2 or greater) the next line to execute is the for loop. It will iterate over the range 2…x and will run the line in its code block on every iteration. If x is divisible by n, then this statement will exit the function with a return value of False.

Assuming the loop runs successfully through to the end of the range without exiting then we may conclude that x is a prime.

That brings us to the final line of the code sequence,

``````return True
``````

which will be the return value for x==2, or x==prime. The only value that is remembered is the parameter, `x` and it is only used for testing, not the return. The return in either case will be a boolean.

As an aside, any time we see a function with `is_` in the name, we can expect that function to return a boolean.

``````is_something(x)
``````

returns True if x is that something, else False.

Thank you very much. I think I understand now.

1 Like

def is_prime (x):
if x < 2:
print x, “is not a prime”
return False
else:
for n in range (2, x-1):
if x == 2:
print x, “is a prime”
return True
elif x % n == 0:
print x, “is not a prime”
return False
else:
print x, “is a prime”
return True

is_prime(13)
is_prime(10)
is_prime(2)

Why does my code fail on is_prime(2)?

If x == 2, then the expression`for n in range (2, x-1)` evaluates to `for n in range (2, 1)`, which yields no output:

``````for n in range(2, 1):
print("in loop")
print(n)
print("loop complete")

# Output:
loop complete
``````

Thank you for the explanation. I just solved the problem, and your reply helped.

1 Like

def is_prime(x):
while x > 1:
a = 1
n = 0
if x % a == 0:
n = n+1
a = a+1
if n == 2:
return True
else:
return False
What is my fault??

When does `x` change?

example:

print is_prime(15)

I suspect that both returns revolve around the value of `n` being `2`, so the loop never completes. When that conditional is removed you will have an infinite loop.

Consider very carefully how to use `while` safely, else go with a loop that is less likely to be infinite, such as `for`.

thanks a lot  1 Like

def is_prime(x):
if x == 2:
return True
if x < 2:
return False
else:
j = 0
for n in range (2, x):
if x % n == 0:
return False
return True
print is_prime(2)
print is_prime(3)
print is_prime(4)
print is_prime(5)
print is_prime(6)
print is_prime(7)
print is_prime(8)
print is_prime(9)

Somehow I am having trouble trying to tackle multiples. Currently, my biggest pain has been number 9. Could someone explain what I did wrong or what my code is doing for reading 6 as not prime, but 9 as such?