Skip to content

Instantly share code, notes, and snippets.

@komasaru

komasaru/prime_number.py

Last active Dec 25, 2017
Embed
What would you like to do?
Python script to judge prime number.
#! /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