Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions maths/prime_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Prime numbers calculation."""


def primes(max: int) -> int:
"""
Return a list of all primes up to max.
>>> primes(10)
[2, 3, 5, 7]
>>> primes(11)
[2, 3, 5, 7, 11]
>>> primes(25)
[2, 3, 5, 7, 11, 13, 17, 19, 23]
>>> primes(1_000_000)[-1]
999983
"""
max += 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add type hints and doctests please as discussed in CONTRIBUTING.md

numbers = [False] * max
ret = []
for i in range(2, max):
if not numbers[i]:
for j in range(i, max, i):
numbers[j] = True
ret.append(i)
return ret


if __name__ == "__main__":
print(primes(int(input("Calculate primes up to:\n>> "))))