Skip to content
playdsa
Preferences

Make yourself comfortable.

Saved on this browser. Your device’s reduced-motion preference is always respected.

Theme
Advanced settings

Boss fightThe Prime Sieve

Learn
Play
Prove
Problem context and objectives
Mission briefing

Puzzle systems engineer · A game mechanic is behaving incorrectly

Cross out composites from each smallest factor: The Prime Sieve.

Brute force may pass the demo but fail when the world fills with players.

How you win

  1. 1Recognize when Sieve of Eratosthenes matches the clues
  2. 2Keep this true after every move: before base p, every composite below p squared is already marked
  3. 3Reach the result within O(n log log n) time and O(n) space

Rules and pressure

  • Target cost: O(n log log n) time and O(n) space
  • State rule: before base p, every composite below p squared is already marked
Lesson 1 of 3

Live algorithm trace

Sieve of Eratosthenes

Complete execution
1 of 5

Mark every candidate from two through nine as provisionally prime.

1prime flags start true from 2
2for base while base squared < n
3if base is still prime
4mark multiples from base squared
5count remaining true flags
6return count
n = 10candidates = 2..9
Truth to preserve / Cost target
Truth to preserve

before base p, every composite below p squared is already marked

Cost target

O(n log log n) time and O(n) space

Assume every integer from two is prime. For each still-prime base, mark multiples beginning at base squared because smaller multiples already had a smaller factor.

Your call · What should guide every step of this algorithm?

Help shape PlayDSA

Something confusing, broken, or missing? Leave a quick note without leaving your lesson.

Please leave out passwords, payment details and other private information.

Page included: /

Sign in to save feedback here, or send it with your email app. Your draft stays here while you sign in.

Open email instead