python - Why is my modulo condition in my prime number tester not working? -


i'm trying (and failing) write simple function checks whether number prime. problem i'm having when if statement, seems doing same thing regardless of input. code have:

def is_prime(x):     if x >= 2:         in range(2,x):             if x % != 0:    #if x / remainder other 0                 print "1"                 break             else:                 print "ok"         else:             print "2"     else: print "3"  is_prime(13) 

the line comment i'm sure problem is. prints "1" regardless of integer use parameter. i'm sorry stupid question, i'm not experienced programmer @ all.

your code close being functional. have logical error in conditional.

there optimizations can make primality test checking until square root of given number.

def is_prime(x):     if x >= 2:         in range(2,x):             if x % == 0: # <----- need checking if evenly                 print "not prime" # divisible , break if since means                 break             # number cannot prime             else:                 print "ok"         else:             print "prime"     else:         print "not prime" 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -