#C11605. Generate Prime Numbers using the Sieve of Eratosthenes
Generate Prime Numbers using the Sieve of Eratosthenes
Generate Prime Numbers using the Sieve of Eratosthenes
You are given a single integer n
. Your task is to generate and output all prime numbers less than or equal to n
using the Sieve of Eratosthenes algorithm. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
If n < 2
, output nothing.
Note: The expected output should be the prime numbers separated by a single space on one line. Ensure that your solution reads input from stdin and writes the answer to stdout.
Mathematical Formulation:
For a given integer \( n \), let \( S = \{2, 3, \dots, n\} \). We remove all multiples of each prime number \( p \) starting from \( p^2 \) until \( n \). The remaining numbers in \( S \) are the primes up to \( n \).
inputFormat
The input consists of a single line containing one integer n
(\( -10^5 \leq n \leq 10^5 \)).
outputFormat
Output a single line containing all prime numbers less than or equal to n
, separated by a single space. If there are no primes, output an empty line.
10
2 3 5 7
</p>