one-file-projects/check_prime.py
2015-04-22 18:33:54 +02:00

30 lines
585 B
Python

from sys import argv
def binomcoeff(n,k):
nom = 1
denum = 1
for i in range(n-k):
nom *= (n-i)
denum *= (i+1)
v = nom//denum
return v
def is_prime(n):
if n <= 1:
return False
for k in range(1,n):
bc = binomcoeff(n,k)
r = bc % n
# print("%d %% %d = %d" % (bc,n,r))
if r != 0:
return False
return True
if len(argv) > 1:
p = int(argv[1])
if is_prime(p):
print("%d is a prime" % p)
else:
print("%d is not a prime" % p)
else:
print("%s <number>" % argv[0])