The sieve of Eratosthenes in F
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The Sieve of Eratosthenes is a classical algorithm used for finding all prime numbers up to a specified integer `n`. Named after the ancient Greek mathematician Eratosthenes, the algorithm efficiently marks non-prime numbers (composite numbers) in a list, leaving the primes unmarked. This algorithm is particularly appreciated for its simplicity and efficiency, especially for large values of `n`.
In this article, we will explore how to implement the Sieve of Eratosthenes in F#, a functional-first language on the .NET platform. We'll provide an in-depth explanation of the algorithm, example code, and a discussion of its efficiency.
The Algorithm
The Sieve of Eratosthenes algorithm can be explained in the following steps:
- Create a list `L` of consecutive integers from 2 to `n`.
- Initialize an empty list, `P`, to store the prime numbers.
- Start with the first number in the list (`p = 2`).
- Move `p` to the list `P`.
- Remove all multiples of `p` from `L`.
- Find the next number in `L`, which becomes the new `p`.
- Repeat steps 4 to 6 until `p^2 > n`.
- At the end of the process, all remaining numbers in `L` are prime, and `P` contains all primes up to `n`.
F# Implementation
Here's how you can implement the Sieve of Eratosthenes in F#:
- Step 1: We start by creating a list of numbers from 2 to `n`.
- Recursive Function: The `sieve` function is recursive, taking `primes` and `numbers` as arguments.
- Base Case: When the list `numbers` is empty, `primes` is returned.
- Filter Multiples: At each step, the head of the list (`p`) is assumed to be prime, and all its multiples are filtered out using the `List.filter` function.
- Stop Condition: The algorithm stops when `p^2 > n`, which ensures all remaining numbers are prime.
- Final Output: The `List.rev` function is used to reverse the primes list since elements are prepended in reverse order during recursion.
- Segmented Sieve: Useful for very large `n`, it limits memory usage by processing smaller intervals.
- Bit Manipulation: Reduces the space requirement from to by employing bits to mark primes.
- Wheel Factorization: Further reduces operations by considering only numbers that are coprime with small primes.
Related reading
- The simplest algorithm for poker hand evaluation
- The time complexity of counting sort
- Three Way Merge Algorithms for Text
- Tickmark algorithm for a graph axis
- Tie breaking in a priority queue using python
- Time complexity analysis for finding the maximum element
- Time complexity deleting element of deque
- Time complexity for a very complicated recursion code

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.