Classic algorithms · 14 / 22 Exercise Medium

Sieve of Eratosthenes

Objective

Create a Python program that gets all the prime numbers up to n with the sieve of Eratosthenes.

Instructions

  • Read an integer n greater than or equal to 2.
  • Create a list of booleans is_prime with n + 1 positions, all True except 0 and 1.
  • For each i from 2 while i * i <= n: if is_prime[i] is true, mark its multiples as False, starting at i * i and going up in steps of i.
  • Show the primes separated by spaces on the first line and Total: K on the second.
Input
30
Output
2 3 5 7 11 13 17 19 23 29
Total: 10

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise