File tree Expand file tree Collapse file tree 1 file changed +12
-12
lines changed Expand file tree Collapse file tree 1 file changed +12
-12
lines changed Original file line number Diff line number Diff line change 1
- from typing import List
1
+ from typing import List , Generator
2
2
3
3
4
- def primes (max : int ) -> List [int ]:
4
+ def primes (max : int ) -> Generator [int ]:
5
5
"""
6
6
Return a list of all primes numbers up to max.
7
7
>>> primes(10)
@@ -13,16 +13,16 @@ def primes(max: int) -> List[int]:
13
13
>>> primes(1_000_000)[-1]
14
14
999983
15
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
16
+ numbers : Generator = (i for i in range (1 , (max + 1 )))
17
+ for i in filter (lambda x : x > 1 , numbers ):
18
+ for j in range (2 , i ):
19
+ if (i % j ) == 0 :
20
+ break
21
+ else :
22
+ yield i
25
23
26
24
27
25
if __name__ == "__main__" :
28
- print (primes (int (input ("Calculate primes up to:\n >> " ))))
26
+ number = int (input ("Calculate primes up to:\n >> " ))
27
+ for ret in primes (number ):
28
+ print (ret )
You can’t perform that action at this time.
0 commit comments