Skip to content

Instantly share code, notes, and snippets.

@primus-lab
Last active December 31, 2019 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save primus-lab/7e2291624810f272a87eed403e16e95c to your computer and use it in GitHub Desktop.
Save primus-lab/7e2291624810f272a87eed403e16e95c to your computer and use it in GitHub Desktop.
Prime number generator.
# Author: Pedja
print(" ***** PROTOS *****\n\n\n")
while True:
n=int(input("Enter the ordinal number : "))
def lcm(p,q):
p, q = abs(p), abs(q)
m = p * q
if not m: return 0
while True:
p %= q
if not p: return m // q
q %= p
if not q: return m // p
def nthprime(n):
b1=2
b2=2
x=2
k=3
m=2
while m<=n:
b3=b1+lcm(k-1,b1)
a=b3/b1-1
k=k+1
b1=b2
b2=b3
if x<a:
x=a
m=m+1
return int(x)
if n<1:
print("Number must be greater than zero")
elif n==1:
print("The nth prime is 2")
else:
print("The nth prime is "+str(nthprime(n)))
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