Finding prime numbers is a common task in mathematics and computer science. Prime numbers have fascinated mathematicians for centuries, and they play a crucial role in many fields, including cryptography and number theory. In this blog post, we will explore the Sieve of Eratosthenes algorithm and its implementation using a Python code. It is simple and one of the efficient algorithm for finding prime numbers up to a given number.
The Sieve of Eratosthenes is an ancient algorithm developed by the Greek mathematician Eratosthenes around 200 BCE. It is a systematic method for finding all prime numbers up to a given limit. The algorithm works by iteratively marking the multiples of each prime number, starting from 2, as composite (i.e., not prime). The unmarked numbers that remain are prime.
Let’s break down the steps of the Sieve of Eratosthenes algorithm:
Now, let’s see how we can implement the Sieve of Eratosthenes algorithm using Python code:
def SieveOfEratosthenes(num):
# Create a boolean array 'prime' with True values for indices 0 to num
prime = [True for i in range(num + 1)]
p = 2
# Mark multiples of each prime number as False
while p * p <= num:
if prime[p]:
for i in range(p * p, num + 1, p):
prime[i] = False
p += 1
# Print the prime numbers
for p in range(2, num + 1):
if prime[p]:
print(p)
if __name__ == '__main__':
num = 40
print("Following are the prime numbers smaller than or equal to", num)
SieveOfEratosthenes(num)
Let’s break down the code:
– We start by creating a list called `primes` with `limit + 1` elements, all initialized as `True`. We set the first two elements (`primes[0]` and `primes[1]`) to `False` because 0 and 1 are not prime numbers.
– We iterate over the numbers from 2 to the square root of the given limit. For each prime number, we mark its multiples as `False` in the `primes` list.
– Finally, we create a new list called `prime_numbers` and populate it with the indices of the `primes` list that are still `True`.
prime
with True
values for indices 0 to num
.p
to 2 (the first prime number).p * p
is less than or equal to num
: prime[p]
is True
, we mark all multiples of p
as False
.p
.prime[p]
is still True
).#Output of above code
Following are the prime numbers smaller than or equal to 40
2
3
5
7
11
13
17
19
23
29
31
37
The Sieve of Eratosthenes algorithm is a powerful and efficient method for finding all the smaller prime numbers. By iteratively marking the multiples of each prime number, we can quickly identify all the prime numbers up to a given limit. Python provides a simple and elegant way to implement this algorithm, making it accessible to programmers of all levels.
Remember, the beauty of mathematics lies not only in its elegance but also in its practical applications. Happy coding!
The Sieve of Eratosthenes is a popular algorithm used to find all prime numbers up…
Introduction Have you ever wondered about numbers that don't quite fit in with the rest?…
Struggling to remember the capitals of countries? Well, you’re not alone. Today, we’re going to…
Works with Microsoft® Word® 2010, 2013, 2016, 2019, 2021, 365 and above Upside down question mark (¿)…
We know an Excel function "FORMULATEXT" which converts Formula into Text. What if you want…
Microsoft Word has different ways to add symbol. Nabla symbol (∇) is one of the…