F#
Sieve of Eratosthenes
Functional Programming
Prime Numbers
Algorithm

The sieve of Eratosthenes in F

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

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:

  1. Create a list `L` of consecutive integers from 2 to `n`.
  2. Initialize an empty list, `P`, to store the prime numbers.
  3. Start with the first number in the list (`p = 2`).
  4. Move `p` to the list `P`.
  5. Remove all multiples of `p` from `L`.
  6. Find the next number in `L`, which becomes the new `p`.
  7. Repeat steps 4 to 6 until `p^2 > n`.
  8. 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 O(n)O(n) to O(n/2)O(n/2) by employing bits to mark primes.
  • Wheel Factorization: Further reduces operations by considering only numbers that are coprime with small primes.

Course
Intermediate
27 lessons
15 hours
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 course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms