Practice makes perfect excersice 6 -python


#1

from math import *
def is_prime (x):
n = 1
cnt = 0
if x == 0 or x ==1 or x ==4:
return False
if x == 2 or x ==3:
return True

while n<= ceil(sqrt(x) and x > 4):
    if (x / n) - ceil(x/n) == 0:
        cnt += 1
        if cnt == 2:
            return False 
            break
    n+=1
else:
    return True

can someone explain to me why this function wont return true for prime numbers and false for numbers that could be divided


#2

That expression looks a little bit off.

That is a lot of literals.

if x < 2: return False

covers everything below 2. Start with n = 3 for your loop and see what happens.

Then there is the problem of,

This will be the case with all numbers at some point. A rethink is in order.


#3

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