-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
improved prime numbers implementation #1606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
66db8d7
4f1cb27
9a1befa
7e892bb
f2d7c5e
369a169
7c1f27e
284832b
6c1bd33
c60a71b
41ae8bc
2a539aa
609fb05
b5f294b
fea8077
266ad42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from typing import List | ||
from typing import Generator, List | ||
|
||
|
||
def primes(max: int) -> List[int]: | ||
def primes(max: int) -> Generator[int, None, None]: | ||
""" | ||
Return a list of all primes numbers up to max. | ||
>>> primes(10) | ||
|
@@ -13,16 +13,16 @@ def primes(max: int) -> List[int]: | |
>>> primes(1_000_000)[-1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please change this to list(primes(1_000_000))[-1] so this doctest passes. Please add doctests for list(primes(0)), list(primes(-1)), list(primes(-10)) You can try these on your own computer with: |
||
999983 | ||
""" | ||
max += 1 | ||
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 | ||
numbers: Generator = (i for i in range(1, (max + 1))) | ||
for i in filter(lambda x: x > 1, numbers): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for i in [x for x in numbers if x > 1]: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. filter returns an iterator, better then a full list, i guess There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for i in (x for x in numbers if x > 1): |
||
for j in range(2, i): | ||
if (i % j) == 0: | ||
break | ||
else: | ||
yield i | ||
|
||
|
||
if __name__ == "__main__": | ||
print(primes(int(input("Calculate primes up to:\n>> ")))) | ||
number = int(input("Calculate primes up to:\n>> ")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As discussed in CONTRIBUTING.md, use str.strip() to gracefully deal with leading or trailing whitespace in user input. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
for ret in primes(number): | ||
print(ret) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change this to list(primes(10)) so this doctest passes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed