# Why does is_prime fail for some numbers?

#29

The effective range has been well discussed on the forums, and yes, the square root is all we need, plus 1 so the top value is in the range. It is not the half way point, though, but the crossover point if we take a list of factors and overlay it with a reverse of that list. Moot, though. Good that you picked up on this.

I agree that we should only iterate over odd numbers, and we can go down this rabbit hole to elliminate numbers divisible by 3, 5, 7, etc. Ultimately this will reduce to a range that includes only Prime numbers. We are a long way from working on that level of complexity. First we walk, then we run. Don’t let this distract you from the lesson plan, but add it to the back burner for extra study when you complete the track.

#30
``````File "python", line 10, in <module>
File "python", line 6, in is_prime
ZeroDivisionError: integer division or modulo by zero
``````

Is the error for this code

``````def is_prime(x, n):
if x < 2:
return False
for n in range(x):
print n
if x%n >= 1:
return False
else:
return True
print is_prime(10, 2)
``````

I don’t know why it’s dividing by zero. I tried to make it start at 2.

#31

When the start value is omitted, Python inserts a zero.

``````x % n
``````

is still division by zero when n is `0`.

#32

I tried to call is_prime with the arguments 10 (for x) and 2 (for n), but that doesn’t fly, so I guess I’ll adjust it to one argument

New code defines n globally, but it still threw an error

``````Your function fails on is_prime(1). It returns None when it should return False.
``````
``````n = 2

def is_prime(x):
if x%n == 0:
return False
while n < (x/2):
print n
if x%n >= 1:
return False
else:
return True
is_prime(10)
``````

#33

You won’t need any global variable. Use a for loop and range, starting with 2.

``for n in range(2, x):``

#35

So here it is now

``````Your function fails on is_prime(1). It returns None when it should return False.
``````
``````def is_prime(x):
n = 2
if x%n == 0:
return False
for n in range (2, x):
if x%n >= 1:
return False
else:
return True
is_prime(10)
``````

#36

Recall that we stated no globals are needed. There is no `n` until the for loop. Only `x`.

``````if x < 2: return false
``````

Not needed.

Think back to what is a prime number? Brute force it to check `x`.

Pay special mind to the fact that divisibility may occcur somewhere in the loop so there is no else branch on the if statement.

#37

Haha, I’m afraid I don’t understand. How do I iterate X instead of N, when including both arguments?

#38

`n` is the block parameter of the for loop.

``for n in range(2, x):``

#39

So, aren’t I already iterating x?

#40

Can’t say for sure until we see your current code. Consider that since this is brute force code, optimization is not a concern. Neither is divisibility by 2. That gets done in the first iteration of the loop. Even numbers are kicked out immediately. If we use a range the 2 gets kicked out of the loop. It never happens.

There is one other deficiency, as it were, in your code. What do we know of negative prime numbers? What is the smallest Prime.

#41
``````def is_prime(x):
n = 2
if x%n == 0:
return False
for n in range (2, x):
if x%n >= 1:
print str(x)+" is not prime!"
return False

``````

#42

As mentioned earlier, not needed. What is needed is a domain.

… a natural number greater than 1.

The first natural number greater than 1 is 2. So to constrain our domain we would filter out inputs less than 2. Make sense? How are we going to do that, as a first step?

#43

Well, I put in x > 1 to try to filter out

How do I include a domain?

#44

But you did that inside the loop, which is a repeated step that has mixed meaning. We need only validate x once.

``````if x < 2: return False
``````

Then jump straight into the for loop.

#45
``````def is_prime(x):
n = 2
if x < 2:
return False
for n in range (2, x):
if x%n == 0:
return False
if x%n >= 1:
print str(x)+" is not prime!"
return False
``````

It’s indented properly, just comes out weird on here

#46

Please, once and for all get rid of that line. It has no meaning.

We need only test for divisibility once, and move on. Inside the loop it is impossible to conclude that something is prime, only that it is not prime.

#47
``````def is_prime(x):
if x < 2:
return False
if x%n == 0:
return False
print str(x)+"is Prime!"
for n in range (2, x):
if x%n >= 1:
print str(x)+" is not prime!"
return False

``````

I get a ‘n is referenced before assignment’ so I made n = 1 again before reference, and now it says “is_prime(2) returns False when it should return True”

#48

Now you’re messing with us. I’m out.

#49

Honestly not trying to