Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 'Sieve of Eratosthenes' #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Prime Numbers/sieve_of_eratosthenes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Python program to print all prime number smaller than or equal
to the given input using Sieve of Eratosthenes.
"""


def sieve_of_eratosthenes(num):
# List to store whether a number is prime or not
prime = [True for i in range(num+1)]
p = 2
while (p*p <= num):
# If prime[p] is True, then p is a prime number
if (prime[p] == True):
# Update all multiples of Prime p
for i in range(p*p, num+1, p):
prime[i] = False
p += 1

# Print all prime numbers
for p in range(2, num+1):
if prime[p]:
print(p),


if __name__ == '__main__':
num = int(input())
print("Following are the prime numbers smaller than or equal to {}".format(num))
sieve_of_eratosthenes(num)