Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import math
import sys
import time

magenta = '\u001b[35m'
cyan = '\u001b[36m'
red = '\u001b[31m'

def typer(words):
  for char in words:
    if char == "":
      sys.stdout.write(char)
      sys.stdout.flush()
    else:
      sys.stdout.write(char)
      sys.stdout.flush()

def get_primes(n):
  m = n+1
  numbers = [True for i in range(m)]
  for i in range(2, m):
      for j in range(i*i, m, i):
        numbers[j] = False
  primes = []
  for i in range(2, m):
  	if numbers[i] == True:
  		primes.append(i)
  print(cyan + "\nSieve of Eratosthenes Method - The Prime Numbers Up To " + str(upperlimit) + ": \n\n" + str(primes))
  
def isprime(x):
    for y in range(2,x):
        if (x%y == 0):
            return False
            break
    return True
  
typer("Welcome to the Prime Number Generator! Created by TheCoiledViper.\n")

while True:
    try:
        mode = int(input(magenta + "\n1. Print All Prime Numbers Up to a Given Number.\n2. See If a Single Number is Prime.\n   Enter Your Choice (1/2): "))
        if mode > 2:
        	print(red + "   Nice try! Enter a Valid Number!")
        else:
        	break
    except ValueError:
        print(red + "   Enter a Valid Option!")
        continue
  
if mode == 1:  
	while True:
		try:
			while True:
				primemethod = int(input(cyan + "\n   -Printing Prime Numbers-\n1. Brute Force Method\n2. Sieve of Eratosthenes Method\n   Enter Your Choice (1/2): "))
				break
		except ValueError:
				print(red + "   Enter A Valid Option!")
				continue
		if primemethod == 1:
			while True:
				try:
					upperlimit = int(input(magenta + "\nEnter the Upper Limit for the Prime Number Generator: "))
					break
				except ValueError:
					print(red + "Enter a Valid Integer!")
					continue
						
			print(cyan + "\nBrute Force Method - The Prime Numbers Up To " + str(upperlimit) +":")
			bruteforceprime = []
			for i in range(2,upperlimit + 1):
				if (isprime(i)):
					bruteforceprime.append(i)
			print (bruteforceprime)
				
		elif primemethod == 2:
			while True:
				try:
					upperlimit = int(input(magenta + "\nEnter the Upper Limit for the Prime Number Generator: "))
					break
				except ValueError:
					print(red + "Enter a Valid Integer!")
					continue
			get_primes(upperlimit)
				
		else:
				print(red + "   Enter a Valid Option!")
				continue
		
elif mode == 2:
	while True:
		while True:
			try: 
				testnumber = int(input(magenta + "\n-Prime Number Testing-\nEnter an Integer Which you Would Like to Test: "))
				break
			except ValueError:
				print(red + "Enter a Valid Integer!")
				continue
		if (testnumber >= 2):
			 if (isprime(testnumber)) == True:
			 	print(cyan + str(testnumber) + " is a Prime Number!")
			 elif (isprime(testnumber)) == False :
			 	print(cyan + str(testnumber) + " is NOT a Prime Number!")
		else:
			  print(cyan + str(testnumber) + " is NOT a Prime Number!")
		continue
		  
	
   
			
	

Python 3.6.1 (default, Dec 2015, 13:05:11) [GCC 4.8.2] on linux