Last active
December 25, 2017 03:51
Python script to judge 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
#! /usr/local/bin/python3.6 | |
""" | |
Prime number judgement. | |
""" | |
import math | |
import sys | |
import traceback | |
class PrimeNumber: | |
def is_prime(self, n): | |
try: | |
res = any(n % i == 0 for i in range(2, int(math.sqrt(n)) + 1)) | |
return not(res) | |
except Exception as e: | |
raise | |
if __name__ == '__main__': | |
if len(sys.argv) < 2: | |
print("USAGE: ./prime_number.py N") | |
sys.exit(0) | |
try: | |
n = int(sys.argv[1]) | |
if n < 2: | |
print("Should be integers greater than 1.") | |
sys.exit(0) | |
obj = PrimeNumber() | |
judge = "" if obj.is_prime(n) else "NOT " | |
print("{} : {}PRIME NUMBER".format(n, judge)) | |
except Exception as e: | |
traceback.print_exc() | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment