6. is_prime


#1

Can someone help me with this code? It keeps telling me that 9 reads true when it should return false.....

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

Replace this line with your code.


#2

can you rewrite your code !!
use this infos


#3

you miss tab here or no ?


#4

I tried it with and without. I ended up going a really long about way of things... like adding an elif x % 3 == 0 return False and elif x == 5 return True, and then elif x % 5 == 0 return False..

I'm still not sure what was wrong with the original. I tried moving the else statement into different tab spaces and it kept giving me different errors.


#5

can you use elif x>3 instead of else: and if x>3:
try it


#6

i am very confused as in mathe i am dummy.

i did smth like this but receive error.
def is_prime(x):
[tab]for n in range(2):
[tab][tab]n < 2
[tab][tab]if x % n == 0:
[tab][tab][tab]return False
[tab][tab]else:
[tab][tab]return True
print is_prime(33)

i searched liitbe bit and found some code cheated liitle bit on it and passed challenge but i definitely dont understand whats going on. can some genius write comment to code so that i understand it? thanks

def is_prime(x):
if x < 2:
return False;
if x % 2 == 0:
return x == 2
y = 3
while y*y <= x:
if x % y == 0:
return False
y += 2
return True
print is_prime(4)


#7

@coding_botan

Cheating isn't the best way to understand you how to it

def is_prime(x):
    if x < 2:
          return False;
          if x % 2 == 0:
               return x == 2
               y = 3
               while y*y <= x:
                   if x % y == 0:
                       return False
                       y += 2
                   return True
print is_prime(4)

is this how you indent it?


#8

no your indent gives error.

def is_prime(x):
----if x < 2:
--------return False;
----if x % 2 == 0:
--------return x == 2
----y = 3
----while y*y <= x:
--------if x % y == 0:
------------return False
--------y += 2
----return True
print is_prime(4)


#9

@coding_botan

def is_prime(x): #we create a function called prime with x as argument
    if x < 2: # if x is smaller than two,
       return False; #we return false because one and two is not prime
    if x % 2 == 0: #if x is evenly divisible by 2
       return x == 2 #we return true or false if x is equal to 2
    y = 3 #we create a variable called y with 3 as value
    while y*y <= x: #as long as the product of y times y is smaller or equal to x
        if x % y == 0: #we test if x is evenly divisible by three
           return False #if yes we return false because it's not prime
        y += 2 #otherwise we add 2 to y variable
    return True #if x is prime we return true
print is_prime(4) #we test if 4 is prime

#10

amazing commenting :hamburger:


#11

I used if/elif/else and for/else, like this:

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

#12

This answer is correct, but I don't think you need the elif section.


#13

Hi! I'm struggling with this problem.
1) Why do you set x to 2?
2) In line 7, why do you multiply y*y? Why does it more work while y range(2,x-1)?
3) In line 10 why do you step up by 2 rather than one?

Thanks! Any feedback is very helpful!


#14

This is the method I used also, but it was failing over and over until I realised that the final else: return True is an else statement for the for loop NOT an else statement for the final if statement.... i.e. my indentation was causing it to fail but I couldn't work out why.


#15

This works:

def is_prime(x):
if x < 2:
return False
elif x == 2 or x == 3:
return True
elif x >= 4:
for n in range(2, x - 1):
print n
if x % n == 0:
return False
elif x % n != 0 and n == x - 2:
return True


#16

I finally get result passed, however, I am a freshman.
Could you please help to check my code ?
I am not convinced about coding and I really have doubt about my code.

The following is my code:
def is_prime(x):
if x ==0 or x==1 :
return False
elif x == 2:
return True
elif x < 0:
return False
else:
for n in range(2,x+1):
print n
print x
print x%n
if x%n ==0 and x!=n:
return False
elif x%n !=0:
continue
elif n == x:
return True

Thank you everyone:)
Woah! The indent is actually correct in edit view while upload to the net, it seems indent disappears.
Well it is no matter and running result is pass on codecademy.


#17

It happens to me too. It's a for/else statement, not an if/else


#21

When I run my code I get the following error:
"Oops, try again. Your function fails on is_prime(-7). It returns True when it should return False.".

Then I had to return False if the number is negative. The problem is the definition of prime number given in the exercise: it doesn't apply just to positive numbers, but to negative as well. So -7 is actually a prime number.


#22

A post was split to a new topic: Why doesn't this code work if x = 9?


#23

Prime factorization of negative numbers cannot be done without fudging, which is possibly why the definition does not include numbers less than 2. If we want a prime to be negative, then multiply it by -1, however that number will not be a prime since it is not a product of 1 and a positive integer greater than 1.