Is_prime


#1

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

error: Oops, try again. Your function fails on is_prime(3). It returns None when it should return True.

hi, don't understand what's worng in my code.
help please?
and another question- i saw in few places the code:
< for n in range(2, x-1):
if n % x == 0: >
and copy it but i don't realy understand it.
thank's!


#2

please read this topic:

kind of boring for the helping to solve the exact same problem thrice in 2 hours.


#3

i already read that topic. it answerd me about the problem in the code, but the second question is steal not clear to me.
What effect this algorithm checks?

< for n in range(2, x-1):
if n % x == 0: >

#4

lets say x is 9, this for loop will loop through 2 till 8 ( 8 not included) (which is is why i don't understand minus one, the stop value is not included in range), for each number between 2 and 8 it will check if the remainder of 9 divide by the range has a remainder of zero. if so, the number is not a prime number, the program returns false


#5

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