Created
February 18, 2012 10:03
-
-
Save abhiomkar/1858565 to your computer and use it in GitHub Desktop.
is Prime Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_prime(x): | |
""" Return True if 'x' is prime number, otherwise, return False """ | |
# positive numbers | |
x = abs(int(x)) | |
# 0, 1 are not prime | |
if x < 2: | |
return False | |
# 2 is primary number | |
if x == 2: | |
return True | |
# even numbers are not prime | |
if not x & 1: | |
return False | |
# range from 3 to only till square root of the target number | |
for i in xrange(3, int(x**0.5) + 1, 2): | |
if x % i == 0: | |
return False | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment