Skip to content

Instantly share code, notes, and snippets.

@primus-lab
Created January 5, 2020 08:08
Show Gist options
  • Save primus-lab/22fdb743d90925ff310c6558b92e5921 to your computer and use it in GitHub Desktop.
Save primus-lab/22fdb743d90925ff310c6558b92e5921 to your computer and use it in GitHub Desktop.
Primality test for numbers of the form N=2*p+1
# Author: Pedja
from sympy import *
print(" ***** SAFE PRIME *****\n\n\n")
while True:
p=int(input("Enter a prime number : "))
def test(p):
if p%6==1:
return "composite"
elif pow(3,p,2*p+1)==1:
return "prime"
else:
return "composite"
if not(isprime(p)):
print("Enter a prime number")
elif p==2:
print("2*"+str(p)+"+1 is prime")
elif p==3:
print("2*"+str(p)+"+1 is prime")
else:
print("2*"+str(p)+"+1 is "+test(p))
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