Skip to content

Commit f069df8

Browse files
SieveOfEratosthenes
Python program to print all primes smaller than or equal to n using Sieve of Eratosthenes
1 parent 99cf7d7 commit f069df8

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎SieveOfEratosthenes‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def SieveOfEratosthenes(n):
2+
3+
# Create a boolean array "prime[0..n]" and initialize
4+
# all entries it as true. A value in prime[i] will
5+
# finally be false if i is Not a prime, else true.
6+
prime = [True for i in range(n+1)]
7+
p = 2
8+
while (p * p <= n):
9+
10+
# If prime[p] is not changed, then it is a prime
11+
if (prime[p] == True):
12+
13+
# Update all multiples of p
14+
for i in range(p * p, n+1, p):
15+
prime[i] = False
16+
p += 1
17+
18+
# Print all prime numbers
19+
for p in range(2, n+1):
20+
if prime[p]:
21+
print p,
22+
23+
# driver program
24+
if __name__=='__main__':
25+
n = 30
26+
print "Following are the prime numbers smaller",
27+
print "than or equal to", n
28+
SieveOfEratosthenes(n)

0 commit comments

Comments
 (0)