Skip to content

Commit 0e82863

Browse files
authored
Merge pull request #9 from cutenti/1_sieve_of_Eratosthenes
Added Sieve of Eratosthenes
2 parents 23b6a31 + 4b658df commit 0e82863

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/intro_1/sieve_of_eratosthenes

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def sieve(n):
2+
"""
3+
Реализация решета Эратосфена.
4+
Функция возвращает массив простых чисел от 1 до n.
5+
"""
6+
7+
prime = [1 for i in range(n + 1)]
8+
prime[0] = prime[1] = 0
9+
10+
num = 2
11+
while num * num <= n:
12+
if prime[num] == 1:
13+
for i in range(num * num, n + 1, num):
14+
prime[i] = 0
15+
num += 1
16+
17+
result = []
18+
19+
for num in range(n + 1):
20+
if prime[num]:
21+
result.append(num)
22+
23+
return result

0 commit comments

Comments
 (0)