💻
Software Development
DSA
DSA
  • Data structure
    • Bit
      • MSB & LSB
  • Arrays
  • Linked List
  • Stack
  • Queue
  • Hash Table
  • Tree
  • Graph
  • Algorithm
    • Bitwise
      • Challenges
    • Array
      • Two pointer
      • Sliding Window
      • Kadane Algorithm
      • Naive Pattern Searching
      • Knuth-Morris-Pratt Algorithm (KMP)
      • Quick Select Algorithm
      • Boyer - Moore Majority Vote Algorithm
      • Floyd's Cycle Detection Algorithm
    • Linked List
      • Challenges
    • Searching
      • Binary Search
        • Challenges
      • Linear Search
      • Depth First Search
      • Breadth First Search
      • Fuzzy Search
      • Rabin-Karp Algorithm
      • A* Algorithm
      • Z Algorithm
    • Sorting
      • Dutch National Flag Algorithm
    • String
      • Manacher Algorithm
    • Gready
      • Knapsack Problem Algorithms
    • Backtracking
    • Tree
      • Moris Traversal
        • Preorder
        • Inorder
    • Graph
      • Biconnected Components
        • Tarjan's Algorithm
      • Kruskal's Algorithm
      • Bellman Ford Algorithm
      • Floyd Warshall Algorithm
      • Topological Sort Algorithm
      • Flood Fill Algorithm
      • Lee Algorithm
      • Boruvka's Algorithm
      • Johnson's Algorithm
      • Kosaraju's Algorithm
    • Rate limitting
      • Token bucket
      • Leaking bucket
      • Fixed window counter
      • Sliding window log
      • Sliding window counter
    • Prioroty Queue
      • Dijkstra's Algorithm
      • Huffman Coding Compression Algorithm
      • Prim's Algorithm
      • kth largest element in an array
    • More
      • Dynamic Programming
      • Pigeonhole Principle
        • Challenges
      • Euclidean algorithm
      • Brian Kernighan’s algorithm
      • Sieve of Eratosthenes Algorithm
      • Sieve of Atkin
      • Luhn Algorithm
      • Maximizing XOR
    • Some pattern to remember when facing the challenges
Powered by GitBook
On this page
  • Explanation with Example:
  • Pseudocode
  • Leetcode problem
  1. Algorithm
  2. More

Sieve of Eratosthenes Algorithm

PreviousBrian Kernighan’s algorithmNextSieve of Atkin

Last updated 19 days ago

Given a number n, print all primes smaller than or equal to n. It is also given that n is a small number.

The sieve of Eratosthenes is one of the most efficient ways to find all primes smaller than n when n is smaller than 10 million or so (Ref ).

Following is the algorithm to find all the prime numbers less than or equal to a given integer n by the Eratosthene’s method: When the algorithm terminates, all the numbers in the list that are not marked are prime.

Explanation with Example:

Let us take an example when n = 100. So, we need to print all prime numbers smaller than or equal to 100.

We create a list of all numbers from 2 to 100

According to the algorithm we will mark all the numbers which are divisible by 2 and are greater than or equal to the square of it.

Now we move to our next unmarked number 3 and mark all the numbers which are multiples of 3 and are greater than or equal to the square of it.

We move to our next unmarked number 5 and mark all multiples of 5 and are greater than or equal to the square of it.

We move to our next unmarked number 7 and mark all multiples of 7 and are greater than or equal to the square of it.

We continue this process, and our final table will look like below:

Pseudocode

algorithm Sieve of Eratosthenes is
    input: an integer n > 1.
    output: all prime numbers from 2 through n.

    let A be an array of Boolean values, indexed by integers 2 to n,
    initially, all set to true.
    
    for i = 2, 3, 4, ..., not exceeding √n do
        if A[i] is true
            for j = i2, i2+i, i2+2i, i2+3i, ..., not exceeding n do
                set A[j] := false

    return all i such that A[i] is true.

Leetcode problem

Wiki
Loading...LeetCode
Logo