forked from joeyajames/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Primes.py
46 lines (33 loc) · 873 Bytes
/
Primes.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
# prime number calculator: find all primes up to n
max = int(input("Find primes up to what number? : "))
primeList = []
for x in range(2, max + 1):
isPrime = True
index = 0
root = int(x ** 0.5) + 1
while index < len(primeList) and primeList[index] <= root:
if x % primeList[index] == 0:
isPrime = False
break
index += 1
if isPrime:
primeList.append(x)
print(primeList)
#-------------------------------------------------------------
# prime number calculator: find the first n primes
count = int(input("Find how many primes?: "))
primeList = []
x = 2
while len(primeList) < count:
isPrime = True
index = 0
root = int(x ** 0.5) + 1
while index < len(primeList) and primeList[index] <= root:
if x % primeList[index] == 0:
isPrime = False
break
index += 1
if isPrime:
primeList.append(x)
x += 1
print(primeList)