Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active December 25, 2017 03:51
Show Gist options
  • Save komasaru/0fb220f1a8a1898b4ae1b26f8125c03c to your computer and use it in GitHub Desktop.
Save komasaru/0fb220f1a8a1898b4ae1b26f8125c03c to your computer and use it in GitHub Desktop.
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