|
1 | 1 | """
|
2 |
| -Sieve of Eratosthenes |
| 2 | +# Sieve of Eratosthenes |
3 | 3 |
|
4 |
| -Input: n = 10 |
5 |
| -Output: 2 3 5 7 |
| 4 | +# Input: n = 10 |
| 5 | +# Output: 2 3 5 7 |
6 | 6 |
|
7 |
| -Input: n = 20 |
8 |
| -Output: 2 3 5 7 11 13 17 19 |
| 7 | +# Input: n = 20 |
| 8 | +# Output: 2 3 5 7 11 13 17 19 |
9 | 9 |
|
10 |
| -you can read in detail about this at |
11 |
| -https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes |
12 |
| -""" |
| 10 | +# you can read in detail about this at |
| 11 | +# https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes |
| 12 | +# """ |
| 13 | + |
| 14 | + |
| 15 | +# def prime_sieve_eratosthenes(num: int) -> list[int]: |
| 16 | +# """ |
| 17 | +# Print the prime numbers up to n |
13 | 18 |
|
| 19 | +# >>> prime_sieve_eratosthenes(10) |
| 20 | +# [2, 3, 5, 7] |
| 21 | +# >>> prime_sieve_eratosthenes(20) |
| 22 | +# [2, 3, 5, 7, 11, 13, 17, 19] |
| 23 | +# >>> prime_sieve_eratosthenes(2) |
| 24 | +# [2] |
| 25 | +# >>> prime_sieve_eratosthenes(1) |
| 26 | +# [] |
| 27 | +# >>> prime_sieve_eratosthenes(-1) |
| 28 | +# Traceback (most recent call last): |
| 29 | +# ... |
| 30 | +# ValueError: Input must be a positive integer |
| 31 | +# """ |
14 | 32 |
|
15 |
| -def prime_sieve_eratosthenes(num: int) -> list[int]: |
16 |
| - """ |
17 |
| - Print the prime numbers up to n |
| 33 | +# if num <= 0: |
| 34 | +# raise ValueError("Input must be a positive integer") |
18 | 35 |
|
19 |
| - >>> prime_sieve_eratosthenes(10) |
20 |
| - [2, 3, 5, 7] |
21 |
| - >>> prime_sieve_eratosthenes(20) |
22 |
| - [2, 3, 5, 7, 11, 13, 17, 19] |
23 |
| - >>> prime_sieve_eratosthenes(2) |
24 |
| - [2] |
25 |
| - >>> prime_sieve_eratosthenes(1) |
26 |
| - [] |
27 |
| - >>> prime_sieve_eratosthenes(-1) |
28 |
| - Traceback (most recent call last): |
29 |
| - ... |
30 |
| - ValueError: Input must be a positive integer |
31 |
| - """ |
| 36 | +# primes = [True] * (num + 1) |
32 | 37 |
|
33 |
| - if num <= 0: |
34 |
| - raise ValueError("Input must be a positive integer") |
| 38 | +# p = 2 |
| 39 | +# while p * p <= num: |
| 40 | +# if primes[p]: |
| 41 | +# for i in range(p * p, num + 1, p): |
| 42 | +# primes[i] = False |
| 43 | +# p += 1 |
35 | 44 |
|
36 |
| - primes = [True] * (num + 1) |
| 45 | +# return [prime for prime in range(2, num + 1) if primes[prime]] |
37 | 46 |
|
38 |
| - p = 2 |
39 |
| - while p * p <= num: |
40 |
| - if primes[p]: |
41 |
| - for i in range(p * p, num + 1, p): |
42 |
| - primes[i] = False |
43 |
| - p += 1 |
| 47 | +def sieve(n): |
| 48 | + isprime[0] = isprime[1] = False |
| 49 | + for i in range(2, math.sqrt(n) + 1): |
| 50 | + if isprime[i]: |
| 51 | + primes.append(i) |
| 52 | + for j in range(i*i, n+1, i): |
| 53 | + isprime[j] = False |
44 | 54 |
|
45 |
| - return [prime for prime in range(2, num + 1) if primes[prime]] |
| 55 | +n = int(input()) |
| 56 | +isprime = [True for i in range(n+1)] |
| 57 | +primes = [] |
46 | 58 |
|
| 59 | +print('The prime numbers from 1 to', n, 'are:') |
| 60 | +print(*primes) |
47 | 61 |
|
48 | 62 | if __name__ == "__main__":
|
49 | 63 | import doctest
|
|
0 commit comments