#K2361. Prime Number Generation using the Sieve of Eratosthenes
Prime Number Generation using the Sieve of Eratosthenes
Prime Number Generation using the Sieve of Eratosthenes
Your task is to generate all prime numbers less than or equal to a given integer n using the Sieve of Eratosthenes algorithm.
The Sieve of Eratosthenes is an efficient algorithm to find all primes smaller than or equal to n. It works by marking the multiples of each prime number starting from 2. The numbers which remain unmarked at the end are prime. In mathematical notation, for a given integer \( n \), we need to identify all primes \( p \) such that \( 2 \leq p \leq n \).
After computing the list of primes, you should output them as a space-separated string on a single line.
inputFormat
The input consists of a single integer n
(\( 0 \leq n \leq 10^6 \)), read from standard input.
outputFormat
Output all prime numbers less than or equal to n
as a space-separated string. If there are no primes, output nothing.
10
2 3 5 7