From 18b7bf2a654da541ca3b913e732d6794ddad7b14 Mon Sep 17 00:00:00 2001 From: Prisha Mordia <130738859+Prisha-Mordia@users.noreply.github.com> Date: Sat, 19 Oct 2024 20:37:18 +0000 Subject: [PATCH] add code for sieve of eathosthenes --- Math/sieve_of_eratosthenes.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 Math/sieve_of_eratosthenes.py diff --git a/Math/sieve_of_eratosthenes.py b/Math/sieve_of_eratosthenes.py new file mode 100644 index 0000000..99e3571 --- /dev/null +++ b/Math/sieve_of_eratosthenes.py @@ -0,0 +1,14 @@ +def sieve_of_eratosthenes(n): + prime = [True for _ in range(n + 1)] + p = 2 + while p * p <= n: + if prime[p]: + for i in range(p * p, n + 1, p): + prime[i] = False + p += 1 + primes = [p for p in range(2, n + 1) if prime[p]] + return primes + + +n = int(input("Enter the number up to which you want to find primes: ")) +print("Prime numbers up to", n, "are:", *sieve_of_eratosthenes(n))