# Is_prime

#1

I'm trying to wrap my head around the logic of this test. In my head I'm trying to see if x is greater than 2 so it filters the numbers less than 2 and gives me the ones greater to test. Then to see if x is only divisible itself and 1 I find is hard to write. Also wouldn't "for n in x:" be correct, why the range when I've told it I need numbers greater than 2. Very sorry for my labyrinth of stupidity, I'm trying to apply the problem that's in my head into Python,

``````def is_prime(x):
if x < 2:
return False
elif x > 2:
return True
for n in range(2, x):
if x / x and x / 1:
return True
else:
return False``````

#2

A function ends the moment a return keyword is reached. so your function ends pretty early

recommendation: just get rid of elif x is greater then 2 and return True

now, `13` is a prime number. For our program to determine 13 is a prime number we need to divide 13 by all numbers in range 2 till 12 (inclusive):

``````13 / 2
13 / 3
...
13 / 11
13 / 12``````

If for all this cases the remainder doesn't equal zero, the number is a prime number, and True should be returned

remember the modulo operator? You could use it here

#3

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

My function doesn't end on the first False return, right?

It fails on (4).

#4

much better, but still some improvments needed

``````def is_prime(x):
if x < 2:
return False
else:
return True``````

a function ends the moment a return keyword is reached, so the above code is what currently executes, the loop is never reached

when i said this, i meant it. Get rid of it, don't replace it with else return True. We have determined the number is actually a prime number. We have a if condition to check for numbers lowers then 2 because those can't be prime numbers, but after that, we should have our loop

now your loop, given a function ends the moment a return keyword is reached, so your function ends in the first run/iteration of your loop

But a number is only a prime number if the loop finishes running (this won't happen False is returned), so you want to place return True outside your loop

#5

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

This worked.

Returning False is establishing the statement and returning True is closing them. Would this be right?

If I added print statements to those returns and gave x say, a value of 13, it would print True and show that the loop works.

#6

what? When the if condition is evaluate to False, the function ends as it should given the number is not a prime number

if a number is a prime number, it finishes running the loop, and then sees return True

#7

Sorry I'm getting confused.

When you say it sees return True does it say in the console: "True", it comes up as "None" for me.

So what is telling it that it is a prime number

#8

a function only executes when called, if you want to see if a number is prime number, call the function with the number of which you want to know if its a prime number

#9

with the range 2 to x, what is the last number it reaches?

#10

`range()` does not include its stop value, so the last value reached is x - 1 (so lets say `x=13`, the last number ran by the loop is 12

#11

x wasn't defined yet it gave me errors for finding out that is_prime(4) etc wasn't returning, where was the number 4 coming from?

#12

well, the exercise calls the function mutliply times to validate your function works correctly. so `is_prime(4)` is a test case by the exercise

#13

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