Skip to content

Commit 18b7bf2

Browse files
committed
add code for sieve of eathosthenes
1 parent 95e5546 commit 18b7bf2

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

Math/sieve_of_eratosthenes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def sieve_of_eratosthenes(n):
2+
prime = [True for _ in range(n + 1)]
3+
p = 2
4+
while p * p <= n:
5+
if prime[p]:
6+
for i in range(p * p, n + 1, p):
7+
prime[i] = False
8+
p += 1
9+
primes = [p for p in range(2, n + 1) if prime[p]]
10+
return primes
11+
12+
13+
n = int(input("Enter the number up to which you want to find primes: "))
14+
print("Prime numbers up to", n, "are:", *sieve_of_eratosthenes(n))

0 commit comments

Comments
 (0)