Sieve of Eratosthenes — All Primes up to N
M · mediumPracticemath
Problem
Print all prime numbers from 2 to N using Sieve of Eratosthenes.
Example
Input
20
Output
2 3 5 7 11 13 17 19
Create bool array size N+1. Mark multiples of each prime (starting from 2) as composite.