#C1147. Sieve of Eratosthenes Prime Generator

    ID: 40789 Type: Default 1000ms 256MiB

Sieve of Eratosthenes Prime Generator

Sieve of Eratosthenes Prime Generator

You are required to implement the Sieve of Eratosthenes algorithm to generate all prime numbers up to a given integer ( n ).

The Sieve of Eratosthenes works by initially assuming all numbers are prime and then marking the multiples of each prime (starting from 2) as non-prime. More formally, for each prime ( p ), you mark all the numbers of the form ( p^2, p^2+p, p^2+2p, \ldots ) up to ( n ) as composite. It is sufficient to perform this for all ( p ) up to ( \sqrt{n} ) (i.e., ( p \leq \sqrt{n} )).

For example, if ( n = 10 ), the prime numbers are 2, 3, 5, and 7.

inputFormat

A single integer ( n ) (where ( 0 \leq n \leq 10^6 )) is provided via standard input.

outputFormat

Output a space-separated list of prime numbers less than or equal to ( n ) via standard output. If there are no prime numbers, output nothing.## sample

10
2 3 5 7