Skip to content

Add approaches (Dig Deeper) for sieve - #1024

Merged
rabestro merged 2 commits into
mainfrom
sieve-approaches
Nov 22, 2026
Merged

rabestro merged 2 commits into
mainfrom
sieve-approaches

Conversation

@rabestro

@rabestro rabestro commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Closes #988

sieve's "Dig Deeper" tab is empty on the Scala track. This adds an introduction and three approaches:

Slug Idea
mutable-array an Array[Boolean] of marks, a for with two generators and a guard, multiples from n * n with by n (based on the current example, with a guard against array-size overflow)
immutable-set foldLeft over the numbers up to the square root of the limit, numbers -- (n * n to limit by n)
lazylist a LazyList that generates primes on demand, taken while <= limit

The issue asks to refresh the exercise first. That is already done on main: the suite is generated and the example is Scala 3 with an Array, so this PR only adds .approaches/.

Notes:

  • lazylist is a genuine incremental sieve. A Map holds, for each prime, only its next multiple, and the mark is moved on by addition when the sieve gets there. The text walks through the numbers up to 10.
  • lazylist counts in Long: the square of a prime above 46340 does not fit in an Int, and the list has no limit that would keep the primes below that.
  • The introduction has a section on the "unfaithful sieve" (filter(_ % head != 0)): why it is trial division, with a link to Melissa O'Neill's paper. It says plainly that this solution passes the tests.

Checks

  • Every full solution in the markdown passes sieve's test suite with all 5 tests enabled; so does the unfaithful sieve that the introduction shows.
  • The three approaches give the same primes for the limits 0, 1, 2, 3, 4, 100 and 200000.
  • The code is as scalafmt 3.8.2 leaves it with the track's .scalafmt.conf.
  • The Scaladoc links point at https://www.scala-lang.org/api/current/..., and every external link resolves.
  • bin/configlet lint passes.

🤖 Generated with Claude Code

Three approaches: a mutable Array of marks, an immutable Set with
foldLeft, and an endless LazyList of primes. The introduction explains
why the well-known filter-based solution is trial division.

Closes #988

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@rabestro rabestro self-assigned this Nov 14, 2026
@github-actions

Copy link
Copy Markdown

Hello 👋 Thanks for your PR.

This repo does not currently have dedicated maintainers. Our cross-track maintainers team will attempt to review and merge your PR, but it will likely take longer for your PR to be reviewed.

If you enjoy contributing to Exercism and have a track-record of doing so successfully, you might like to become an Exercism maintainer for this track.

Please feel free to ask any questions, or chat to us about anything to do with this PR or the reviewing process on the Exercism forum.

(cc @exercism/cross-track-maintainers)

@rabestro
rabestro marked this pull request as draft November 14, 2026 22:03
@rabestro
rabestro marked this pull request as ready for review November 22, 2026 16:32
@rabestro
rabestro requested review from a team, IsaacG and glennj November 22, 2026 16:32
@rabestro
rabestro merged commit 73c457a into main Nov 22, 2026
8 checks passed
@rabestro
rabestro deleted the sieve-approaches branch November 22, 2026 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add approaches (Dig Deeper) for sieve

2 participants