Skip to content

Add approaches (Dig Deeper) for difference-of-squares - #1023

Merged
rabestro merged 2 commits into
mainfrom
difference-of-squares-approaches
Nov 22, 2026
Merged

rabestro merged 2 commits into
mainfrom
difference-of-squares-approaches

Conversation

@rabestro

Copy link
Copy Markdown
Member

Closes #987

difference-of-squares' "Dig Deeper" tab is empty on the Scala track. This adds an introduction and three approaches:

Slug Idea
ranges (1 to n).map(i => i * i).sum, and (1 to n).sum squared
formulas n * (n + 1) * (2 * n + 1) / 6, and n * (n + 1) / 2 squared (the current example)
foldleft both sums in one traversal: foldLeft((0, 0)) with a tuple

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

Notes:

  • formulas explains why the multiplication comes before the division: n / 2 * (n + 1) gives 12 for n = 5, not 15.
  • foldleft is a full solution, so a private sums method returns both sums and the three public methods take what they need from the tuple. The text says that only differenceOfSquares gains from the single pass.
  • The introduction has a note that is not in the issue: the square of the sum no longer fits in an Int when n is above 303.

Checks

  • Every full solution in the markdown passes the exercise's test suite with all 9 tests enabled.
  • The code is unchanged by scalafmt 3.8.2 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: ranges with map and sum, the closed formulas, and
both sums in one pass with foldLeft.

Closes #987

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 21:53
@rabestro
rabestro marked this pull request as ready for review November 22, 2026 16:23
@rabestro
rabestro requested review from a team, IsaacG and glennj November 22, 2026 16:23
@rabestro
rabestro merged commit 243c126 into main Nov 22, 2026
8 checks passed
@rabestro
rabestro deleted the difference-of-squares-approaches branch November 22, 2026 16:56
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 difference-of-squares

2 participants