Skip to content

Commit

Permalink
Merge pull request #279 from Prisha-Mordia/main
Browse files Browse the repository at this point in the history
Add code for Sieve of Eratosthenes
  • Loading branch information
Punit-Choudhary authored Oct 19, 2024
2 parents 95e5546 + 18b7bf2 commit 9cdfac9
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Math/sieve_of_eratosthenes.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 9cdfac9

Please sign in to comment.