Skip to content

Commit 4880e54

Browse files
5ur3cclauss
authored andcommitted
Create prime_numbers.py (TheAlgorithms#1519)
* Create prime_numbers.py * Update prime_numbers.py
1 parent bfac867 commit 4880e54

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

maths/prime_numbers.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""Prime numbers calculation."""
2+
3+
4+
def primes(max: int) -> int:
5+
"""
6+
Return a list of all primes up to max.
7+
>>> primes(10)
8+
[2, 3, 5, 7]
9+
>>> primes(11)
10+
[2, 3, 5, 7, 11]
11+
>>> primes(25)
12+
[2, 3, 5, 7, 11, 13, 17, 19, 23]
13+
>>> primes(1_000_000)[-1]
14+
999983
15+
"""
16+
max += 1
17+
numbers = [False] * max
18+
ret = []
19+
for i in range(2, max):
20+
if not numbers[i]:
21+
for j in range(i, max, i):
22+
numbers[j] = True
23+
ret.append(i)
24+
return ret
25+
26+
27+
if __name__ == "__main__":
28+
print(primes(int(input("Calculate primes up to:\n>> "))))

0 commit comments

Comments
 (0)