← All Exercises
Sieve of Eratosthenes
basicpython
Background
The sieve is the baseline prime generator for number theory, hash sizing, and competitive programming.
Problem
Given a non-negative integer n, return all prime numbers less than or equal to n in ascending order.
Input
A single non-negative integer n no larger than 1000000.
Output
An ascending list of primes in the range [2, n], empty when n < 2.
Sample tests
Sample 1
Input: n = 10
Expected: [2, 3, 5, 7]
Sample 2
Input: n = 2
Expected: [2]
Sample 3
Input: n = 1
Expected: []
Your solution
Learning objective
Implement the sieve with a square-root bound and explain its O(n log log n) complexity.