Skip to main content

Sieve of Eratosthenes


The Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to a given limit. It was devised by the Greek mathematician Eratosthenes around 200 BCE, and it is still used today in various applications, such as cryptography and computer science.

The basic idea of the Sieve of Eratosthenes is to eliminate all composite numbers (i.e., non-prime numbers) in a range of numbers from 2 to some upper limit. To do this, we start by marking all the numbers from 2 to the limit as potential primes. We then iterate through the list of numbers, starting with 2, and for each prime number we encounter, we mark all its multiples as composite. This process is repeated for each subsequent prime number until we reach the end of the list.

At the end of this process, all the unmarked numbers that remain are primes. This is because any composite number in the list would have been marked as a multiple of some smaller prime number, and thus eliminated from the list. For example, if we start with the list [2, 3, 4, 5, 6, 7, 8, 9, 10] and apply the Sieve of Eratosthenes, we would mark 4, 6, 8, and 10 as composite (multiples of 2), and 9 as composite (a multiple of 3). The remaining unmarked numbers, 2, 3, 5, and 7, are all prime.

The Sieve of Eratosthenes has a time complexity of O(n log log n), which is very efficient for most practical purposes. However, for extremely large values of n, the algorithm can become impractical, as it requires an array of size n to store the potential primes. In these cases, more advanced algorithms such as the Sieve of Atkin or the AKS primality test may be used instead.

Overall, the Sieve of Eratosthenes is a simple and elegant algorithm for finding primes that has stood the test of time. Its basic principle of eliminating composite numbers through repeated iteration has inspired many other algorithms and mathematical discoveries over the centuries.


ALGORITHM :

The Sieve of Eratosthenes is an algorithm for finding all prime numbers up to a given limit. The algorithm works by eliminating all composite numbers (i.e., non-prime numbers) in a range of numbers from 2 to some upper limit. Here are the steps to execute the algorithm:

  1. Create a list of all the numbers from 2 to the upper limit.
  2. Start with the first prime number, which is 2.
  3. Mark all the multiples of the current prime number as composite.
  4. Find the next prime number by selecting the smallest number in the list that is not yet marked as composite. This number is the next prime number.
  5. Repeat steps 3 and 4 until all the prime numbers up to the limit have been found.
  6. The unmarked numbers that remain in the list are all prime.

PSEUDO CODE :

Here's the pseudo code for the Sieve of Eratosthenes algorithm:

The algorithm takes an integer n as input and returns a list of all prime numbers up to n. The algorithm works by creating an array A of Boolean values, where each element A[i] indicates whether i is a prime number. Initially, all values of A are set to true. The algorithm then iterates through the array from 2 to the square root of n, checking whether each number is prime. If the number is prime, the algorithm marks all of its multiples as composite by setting their corresponding values in A to false. Finally, the algorithm outputs all values of i such that A[i] is true, which are the prime numbers up to n.

PYTHON CODE :

Here's an implementation of the Sieve of Eratosthenes algorithm in Python:

The function takes an integer n as input and returns a list of all prime numbers up to n. The algorithm works by creating a list of all numbers from 2 to n, and then iterating through the list, marking all multiples of each prime number as composite. The algorithm stops when it reaches the square root of n, since any composite number greater than the square root of n must have a factor less than the square root of n. Finally, the function collects all unmarked numbers into a list and returns it as the result.

JAVA CODE :

Here's an implementation of the Sieve of Eratosthenes algorithm in Java:


The sieveOfEratosthenes method takes an integer n as input and returns a list of all prime numbers up to n. The algorithm works by creating an array primes of Boolean values, where each element primes[i] indicates whether i is a prime number. Initially, all values of primes are set to true. The algorithm then iterates through the array from 2 to the square root of n, checking whether each number is prime. If the number is prime, the algorithm marks all of its multiples as composite by setting their corresponding values in primes to false. Finally, the algorithm collects all values of i such that primes[i] is true, which are the prime numbers up to n, and returns them as a list. The main method demonstrates how to use the sieveOfEratosthenes method to find prime numbers up to a given limit.

C CODE :

Here's an implementation of the Sieve of Eratosthenes algorithm in C:


In this implementation, we create a boolean array primes of size n+1, where primes[i] is initially set to 1 for all i. We then iterate over all prime numbers p from 2 up to the square root of n, and for each prime p, we mark all its multiples as non-prime by setting primes[i*p] to 0 for all i >= p*p and i <= n. Finally, we print out all numbers p such that primes[p] is still 1, which correspond to the prime numbers up to n.

C++ CODE :

Here's an implementation of the Sieve of Eratosthenes algorithm in C++:


In this implementation, we create a boolean vector primes of size n+1, where primes[i] is initially set to true for all i. We then iterate over all prime numbers p from 2 up to the square root of n, and for each prime p, we mark all its multiples as non-prime by setting primes[i*p] to false for all i >= p*p and i <= n. Finally, we print out all numbers p such that primes[p] is still true, which correspond to the prime numbers up to n.

JAVASCRIPT CODE :

Here's an implementation of the Sieve of Eratosthenes algorithm in JavaScript:


In this implementation, we create an array primes of boolean values of size n+1, where primes[i] is initially set to true for all i. We then iterate over all prime numbers p from 2 up to the square root of n, and for each prime p, we mark all its multiples as non-prime by setting primes[i*p] to false for all i >= p*p and i <= n. Finally, we create an array result of prime numbers up to n by adding all numbers p such that primes[p] is still true. The function sieve() returns this array.

In the example usage, we call sieve(n) with n = 50, and store the result in the variable primes. We then print out the result using console.log().

Comments

Popular posts from this blog

Selection Sort

Selection sort is a simple sorting algorithm that sorts an array by repeatedly finding the minimum element from the unsorted portion of the array and swapping it with the first element of the unsorted portion. It is called selection sort because it repeatedly selects the smallest element of the unsorted portion of the array and moves it to its correct position in the sorted portion of the array. The time complexity of selection sort is O(n^2), where n is the number of elements in the array. This makes it inefficient for large arrays and it is not a good choice for sorting large datasets. However, selection sort is easy to understand and implement, and it is useful for educational purposes and for small arrays. One of the advantages of selection sort is that it makes fewer swaps than other sorting algorithms such as bubble sort. This makes it more efficient in terms of memory writes, which can be important in some situations. Another advantage of selection sort is that it is a stable so

Counting Sort

Counting sort is a sorting algorithm that operates by counting the frequency of each element in the input array, and then using these frequencies to compute the final sorted output. The algorithm works by first creating an array of counters, with each counter corresponding to a distinct element in the input array. The counters are initialized to zero, and then the input array is scanned once to count the number of occurrences of each element. This count information is stored in the corresponding counter in the counter array. Once the count information is obtained, the algorithm uses it to determine the final position of each element in the sorted output array. This is done by iterating over the counter array, and for each counter value, writing the corresponding element to the sorted output array the appropriate number of times. The time complexity of counting sort is O(n+k), where n is the length of the input array and k is the range of the elements. The space complexity is also O(n+k

Pigeonhole Sort

Pigeonhole sort is a sorting algorithm that works by distributing elements of an input sequence into a set of pigeonholes or buckets. It is an efficient sorting algorithm for small ranges of values where the number of elements to be sorted is roughly the same as the range of values. Pigeonhole sort is an example of a bucket sort algorithm. The pigeonhole sort algorithm works by first determining the range of values in the input sequence. This range is used to create a set of pigeonholes or buckets, each of which can hold one or more elements from the input sequence. Each element in the input sequence is then placed into its corresponding pigeonhole based on its value. If two or more elements have the same value, they can be placed in the same pigeonhole. Once all the elements have been placed into their corresponding pigeonholes, the elements are sorted within each pigeonhole. Finally, the sorted elements are combined into a single output sequence. Pigeonhole sort has a time complexity