Skip to content

Instantly share code, notes, and snippets.

@primus-lab
Created January 5, 2020 18:33
Show Gist options
  • Save primus-lab/ace843faa9f742df0185170f83f594a0 to your computer and use it in GitHub Desktop.
Save primus-lab/ace843faa9f742df0185170f83f594a0 to your computer and use it in GitHub Desktop.
Primality test
# Author: Pedja
print(" ***** BHASKARA *****\n\n\n")
while True:
n=int(input("Enter a number : "))
def test(n):
s=0
for k in range(1,n):
s=s+k
p=1
for k in range(1,n):
p=(p*k)%s
return p%s==n-1
if n<3:
print("Number must be greater than two")
else:
if test(n):
print(str(n)+" is prime")
else:
print(str(n)+" is composite")
try_again = ""
# Loop until users opts to go again or quit
while not(try_again == "1") and not(try_again == "0"):
try_again = input("Press 1 to try again, 0 to exit. ")
if try_again in ["1", "0"]:
continue # a valid entry found
else:
print("Invalid input- Press 1 to try again, 0 to exit.")
# at this point, try_again must be "0" or "1"
if try_again == "0":
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment