Skip to content

Commit 23dde35

Browse files
committed
added version 2: get first n primes
1 parent 7a2ca08 commit 23dde35

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

Primes.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# prime number calculator
1+
# prime number calculator: find all primes up to n
22

33
max = int(input("Find primes up to what number? : "))
44
primeList = []
@@ -18,4 +18,29 @@
1818
primeList.append(x)
1919

2020
print(primeList)
21-
21+
22+
23+
#-------------------------------------------------------------
24+
# prime number calculator: find the first n primes
25+
26+
count = int(input("Find how many primes?: "))
27+
primeList = []
28+
x = 2
29+
30+
while len(primeList) < count:
31+
isPrime = True
32+
index = 0
33+
root = int(x ** 0.5) + 1
34+
35+
while index < len(primeList) and primeList[index] <= root:
36+
if x % primeList[index] == 0:
37+
isPrime = False
38+
break
39+
index += 1
40+
41+
if isPrime:
42+
primeList.append(x)
43+
44+
x += 1
45+
46+
print(primeList)

0 commit comments

Comments
 (0)