Created
April 11, 2013 20:49
-
-
Save tadasv/5367051 to your computer and use it in GitHub Desktop.
Prime check
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
from math import sqrt | |
def is_prime(n): | |
if n is 2 or n is 3 or n is 5: | |
return True | |
if n % 2 == 0 or n % 3 == 0: | |
return False | |
sq = sqrt(n) | |
i = 6 | |
while i < sq: | |
if n % (i - 1) == 0 or n % (i + 1) == 0: | |
return False | |
i += 6 | |
return True | |
if __name__ == '__main__': | |
import sys | |
numbers = sys.argv[1:] | |
if not numbers: | |
print "Usage: {0} num [num1] ...".format(sys.argv[0]) | |
sys.exit(0) | |
for n in numbers: | |
print "{0} {1}".format(n, is_prime(int(n))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment