Sieve of Eratosthenes — All Primes up to N

M · mediumPracticemath

Print all prime numbers from 2 to N using Sieve of Eratosthenes.

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.
← Selection SortSort Array of 0s, 1s and 2s (Dutch National Flag) →
Report an issue with this question