From 4b658dfb0d90abfb849056a7aceac392c55ebcc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D1=80=D1=8C=D1=8F=20=D0=A2=D0=BE=D1=80=D0=B3?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0?= Date: Wed, 10 Dec 2025 06:01:20 +0300 Subject: [PATCH] Added Sieve of Eratosthenes --- src/intro_1/sieve_of_eratosthenes | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/intro_1/sieve_of_eratosthenes diff --git a/src/intro_1/sieve_of_eratosthenes b/src/intro_1/sieve_of_eratosthenes new file mode 100644 index 0000000..e78fcfd --- /dev/null +++ b/src/intro_1/sieve_of_eratosthenes @@ -0,0 +1,23 @@ +def sieve(n): + """ + Реализация решета Эратосфена. + Функция возвращает массив простых чисел от 1 до n. + """ + + prime = [1 for i in range(n + 1)] + prime[0] = prime[1] = 0 + + num = 2 + while num * num <= n: + if prime[num] == 1: + for i in range(num * num, n + 1, num): + prime[i] = 0 + num += 1 + + result = [] + + for num in range(n + 1): + if prime[num]: + result.append(num) + + return result