is_prime


#1



is_prime - Practice Makes Perfect

It says that when checking 0 it fails.

I expect the program to run like it does in my Python GUI editor in sublime text 3. After I copy it into this web form it won't execute. What is off? I have checked other is_prime forums and the logic has similar semantics. Thanks.

In sublime the output for is_prime(9) is:
9 % 2 = 1 is Prime
9 % 3 = 0 is not Prime
9 % 4 = 1 is Prime
9 % 5 = 4 is Prime
9 % 6 = 3 is Prime
9 % 7 = 2 is Prime
9 % 8 = 1 is Prime

def is_prime(x):
	for i in range(2, x, 1):
		if x % i == 0 or i < 1:
		    print x, "%", i, "=", (x % i), "is not Prime"
		    return False
			
		else:
		    print x, "%", i, "=", (x % i), "is Prime"
		    return True
			
is_prime(x)


#2

If x is 0, this is range(2, 0, 1). With 2 beeing the start, 0 the end and 1 the size of each step.
But how do you go from 2 to 0 with positive steps?

Also note that once a return statement is reached the whole function will exit. That means that

if x % i == 0 or i < 1: is False for just one i, True will be returned.


#3

It states "02. For each number n from 2 to x - 1, test if x is evenly divisible by n," so why would you check for zero if it starts from 2?


#4

Because every number is evenly divisble by 1, and division by 0 is undefined.

Note that your function fails when x = 0, but you are talking about n = 0.


#5

try this code below

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


#7

Working variant:
def is_prime(x):
if x < 2:
return False
elif x < 4:
return True
else:
y=2
while y if x % y == 0:
return False
y+=1
else:
return True


#8

Who can help me? Why is_prime(9) is True, I can't find the logical error in my code。
β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
Happy! I have resolved it, because the key-word "return" can only be used once. I just think it could be updated in a loop, so foolish!

def is_prime(x):
if x == 0 or x == 1:
return False
elif x == 2:
return True
else:
for i in range(2, x):
if x % i == 0:
return False
break
else:
return True


#9

my code has similar result with yours.
def is_prime(x):
if x > 3:
for n in range(2,x-1):
if x % n == 0:
return False
else:
return True
elif x == 2 or x == 3:
return True
else:
return False


#10

def is_prime(x):

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

return prost_broj  :wink:

#11

The code below is my final code, and I feel happy to accomplish it.:stuck_out_tongue:

def is_prime(x):
if x <= 1:
return False
elif x == 2:
return True
else:
index = 1
for i in range(2, x):
if x % i == 0:
index = 0
break
else:
index = 1
return bool(index)


#12

this is executable correctly
def is_prime(x):
if x==2:
return True
print "prime"
elif(x>1):
for i in range(2,x-1):
if(x%i==0):

            return False
            break
    else:
        return True

else:
    return False