Skip to content

Instantly share code, notes, and snippets.

@Ge0rg3
Created February 6, 2018 10:23
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 Ge0rg3/3608010b2a1778b30ca01a6ead673613 to your computer and use it in GitHub Desktop.
Save Ge0rg3/3608010b2a1778b30ca01a6ead673613 to your computer and use it in GitHub Desktop.
Prime Number Checker
#imports square root function and sets start var:
from math import sqrt
use = True
counter = 0
#Takes in user's numbers, and "sorts them out":
print("======================")
print("Pryme Number Checker")
print("github.com/georgeomnet")
print("======================\n")
def startprog():
primeinput = input("Enter your prime numbers, all seperated by spaces:\n")
global splitinput
splitinput = primeinput.split()
global elementsinarray
elementsinarray = len(splitinput)
counter = 0
global primeslist
primeslist = []
#Adapted Sieve's Primality Test functions:
def findrange(three, numb, two):
while three < numb:
yield three
three += two
def is_prime(num):
if num == 2:
return True
if (num < 2) or (num % 2 == 0):
return False
return all(num % i for i in findrange(3, int(sqrt(num)) + 1, 2))
#Loops until all numbers are tested:
def whileloop():
global counter
while counter < int(elementsinarray):
if (is_prime(int(splitinput[counter]))) == True:
primeslist.append(int(splitinput[counter]))
counter = counter + 1
#Prints the primes:
def printans():
print("The prime(s) in that list were:")
print(*primeslist, sep=',')
#Asks the user whether or not they would like to go again?
def goagain():
global question
while question != "y" and question != "n":
question = input("\nWould you like to go again? y/n\n")
question = question[0]
question = question.lower()
if question == "y":
print("========================")
elif question == "n":
global use
use = False
else:
print("Invalid answer!")
#Runs all the functions in order to work:
while use == True:
startprog()
counter = 0
whileloop()
printans()
global question
question = ""
goagain()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment