diff --git a/algorithms/dynamic_programming/SieveOfEratosthenes.py b/algorithms/dynamic_programming/SieveOfEratosthenes.py new file mode 100644 index 00000000..e195ab55 --- /dev/null +++ b/algorithms/dynamic_programming/SieveOfEratosthenes.py @@ -0,0 +1,22 @@ +def sieve_of_eratosthenes(n): + + prime = [True for i in range(n+1)] + p = 2 + while (p * p <= n): + + if (prime[p] == True): + + # Update all multiples of p + for i in range(p * 2, n+1, p): + prime[i] = False + p += 1 + + for p in range(2, n): + if prime[p]: + print (p), + +if __name__=='__main__': + n = 30 + print ("Following are the prime numbers smaller"), + print ("than or equal to", n ) + sieve_of_eratosthenes(n) \ No newline at end of file