Is_prime


#1

def is_prime(x):

if x>2 : 
    n=x
    while n>2:
        if x%(n-1)!=0:
           return True
        else :
           return False
        n-=1

elif x==2:
    return True
else :
    return False

Oops, try again. Your function fails on is_prime(4). It returns True when it should return False.


#2

This part will cause your program to fail becase you'll be asking, supposing the value in n is four:

Q: Is 4 % (4 - 1) different than zero?
A: Yes!

#4 % (4 - 1) = 4 % 3 != 0

This could work better if you decreased the value in the variable n before your first if in the while scope.

Hope it helps!


#3

def is_prime(x):

if x>2 : 
    n=x
    while n>2:
        n-=1
        if x%(n-1)!=0:
           return True
        else :
           return False


elif x==2:
    return True
else :
    return False

print is_prime(3)
Oops, try again. Your function fails on is_prime(3). It returns False when it should return True.
It still not work.


#4

I'm completely stuck with this lesson. For some reason I can't get this code working. Has anybody some suggestions on what I'm doing wrong. Thanks in advance!
VBR Patrick


#5

def is_prime(x):
    result = [] #Stores the result
    count = x #To divide x by
    while count - 1 > 1:
        test = x % (count-1) #Calc if x is prime
        count -= 1 
        result.append(test) #Stores the result
        for i in result: #Check if num is composite by looking for evidence in the results
            if i == 0:
                return False
                break
    else:
        if x < 2: 
            return False
        else: # if the num is prime
            return True

Try this it should work because it stores all the reusults rather than one at a time. Just sub var count for the var n in your case


#6

def is_prime(x):
    if x>2 : 
        n=x
        while n>2:
            if x%(n-1)==0:
                return False
                break
            else :
                n -= 1
        else:
            return True


    elif x==2:
        return True
    else :
        return False

Try this instead I made it to where the while statement checks for a composite no. and added an else saying if the while is no longer true (meaning the no. is not composite) then return true (since a non-composite no. greater that 0 is prime)


#7

Change this if x %(n-1) == 0: to if x %(n-1) != 0: and switch line 5 with line 6 so that it is arranged like this

n-=1
if x %(n-1) != 0:

#9

working solution use for loop and else outside it

def is_prime(x):

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

else:
    return True

#10

Really good piece of code. :relaxed:


#11

I think i have an easier way:

def is_int(x):
y = x
y = round(y,0)
if y == x:
return True
else:
return False

print is_int(12.5)


#13

Notice that as long as there is a number n that 'x % n == 0', then x isn't a prime.
Thus the code from line 5-7 should be corrected like this:

if x%(n-1) == 0:
return True
n -= 1

and the False in line 9 should be 'True'


#14

My working solution is below. It loops through every number from 2 up to -x to check whether it is not prime, and if none of the options succeed it finishes the loop returning True. The problem I believe you are having with you code and the problem I had doing this before is that you aren't going through every number to check, it just stops at the first one that it calculates. Remember that anything after a return statement doesn't get calculated.

def is_prime(x):
n = 2
if x < 2:
return False
else:
while n < x:
if x % n != 0:
n += 1
else:
return False
else:
return True


#15

Your code when ran might be a bit problematic because of the control flow,, using two else statement that don't account for any secondary conditions is unnecessary when you can just simplify your code with an if and else statement.


#16

I realised the first else statement is useless, however im sure you are referencing the 2 at the end. Im dont really understand what you are trying to explain, could you change my code to be simpler in the way you suggested?


#17

is_prime

This post should shed some light on a more functional way of writing your code.


#18

I'm not sure why this is not working..

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


#19

This elif has no return statement


#21

Thanks! It works perfectly,


#22

if use this cod I have problem which negative numbers.. ""Oops, try again. Your function fails on is_prime(-10). It returns None when it should return False. ""
May be any know about fix it ?.. thanks


#23

Please post your code