Skip to content

Instantly share code, notes, and snippets.

@abhiomkar
Created February 18, 2012 10:03
Show Gist options
  • Save abhiomkar/1858565 to your computer and use it in GitHub Desktop.
Save abhiomkar/1858565 to your computer and use it in GitHub Desktop.
is Prime Number
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