Skip to content

Commit 369f102

Browse files
Merge pull request #574 from goflora/patch-3
Create sieveOfEratosthenes.py
2 parents fdebb9c + bcd3f21 commit 369f102

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Python/sieveOfEratosthenes.py

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

0 commit comments

Comments
 (0)