From 6902eec17cfd91abc4049e3f38b582902dc9f411 Mon Sep 17 00:00:00 2001 From: Isaac Good Date: Sat, 26 Sep 2026 23:45:51 -0700 Subject: [PATCH] [difference-of-squares] Add new exercise --- config.json | 8 +++ .../.docs/instructions.md | 14 +++++ .../difference-of-squares/.meta/config.json | 19 +++++++ .../difference-of-squares/.meta/example.gd | 16 ++++++ .../difference-of-squares/.meta/template.j2 | 7 +++ .../difference-of-squares/.meta/tests.toml | 37 +++++++++++++ .../difference_of_squares.gd | 10 ++++ .../difference_of_squares_test.gd | 52 +++++++++++++++++++ 8 files changed, 163 insertions(+) create mode 100644 exercises/practice/difference-of-squares/.docs/instructions.md create mode 100644 exercises/practice/difference-of-squares/.meta/config.json create mode 100644 exercises/practice/difference-of-squares/.meta/example.gd create mode 100644 exercises/practice/difference-of-squares/.meta/template.j2 create mode 100644 exercises/practice/difference-of-squares/.meta/tests.toml create mode 100644 exercises/practice/difference-of-squares/difference_of_squares.gd create mode 100644 exercises/practice/difference-of-squares/difference_of_squares_test.gd diff --git a/config.json b/config.json index 8a65a121..96304f67 100644 --- a/config.json +++ b/config.json @@ -250,6 +250,14 @@ "practices": [], "prerequisites": [], "difficulty": 2 + }, + { + "slug": "difference-of-squares", + "name": "Difference of Squares", + "uuid": "1065256b-5263-443f-9f67-28d0fa2e9b93", + "practices": [], + "prerequisites": [], + "difficulty": 2 } ] }, diff --git a/exercises/practice/difference-of-squares/.docs/instructions.md b/exercises/practice/difference-of-squares/.docs/instructions.md new file mode 100644 index 00000000..39c38b50 --- /dev/null +++ b/exercises/practice/difference-of-squares/.docs/instructions.md @@ -0,0 +1,14 @@ +# Instructions + +Find the difference between the square of the sum and the sum of the squares of the first N natural numbers. + +The square of the sum of the first ten natural numbers is +(1 + 2 + ... + 10)² = 55² = 3025. + +The sum of the squares of the first ten natural numbers is +1² + 2² + ... + 10² = 385. + +Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640. + +You are not expected to discover an efficient solution to this yourself from first principles; research is allowed, indeed, encouraged. +Finding the best algorithm for the problem is a key skill in software engineering. diff --git a/exercises/practice/difference-of-squares/.meta/config.json b/exercises/practice/difference-of-squares/.meta/config.json new file mode 100644 index 00000000..ea2f8d0f --- /dev/null +++ b/exercises/practice/difference-of-squares/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "IsaacG" + ], + "files": { + "solution": [ + "difference_of_squares.gd" + ], + "test": [ + "difference_of_squares_test.gd" + ], + "example": [ + ".meta/example.gd" + ] + }, + "blurb": "Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.", + "source": "Problem 6 at Project Euler", + "source_url": "https://projecteuler.net/problem=6" +} diff --git a/exercises/practice/difference-of-squares/.meta/example.gd b/exercises/practice/difference-of-squares/.meta/example.gd new file mode 100644 index 00000000..2def233d --- /dev/null +++ b/exercises/practice/difference-of-squares/.meta/example.gd @@ -0,0 +1,16 @@ +func sum_of_squares(num: int) -> int: + var result: int = 0 + for i in range(num + 1): + result += i * i + return result + + +func square_of_sum(num: int) -> int: + var result: int = 0 + for i in range(num + 1): + result += i + return result * result + + +func difference_of_squares(num: int) -> int: + return square_of_sum(num) - sum_of_squares(num) diff --git a/exercises/practice/difference-of-squares/.meta/template.j2 b/exercises/practice/difference-of-squares/.meta/template.j2 new file mode 100644 index 00000000..6441884a --- /dev/null +++ b/exercises/practice/difference-of-squares/.meta/template.j2 @@ -0,0 +1,7 @@ +{% for case in cases %} +func test_{{ case.func_name }}(solution_script): + var inputs = {{ case.given | togdscript }} + var expected = {{ case.expected | togdscript }} + return [solution_script.{{ case.property | camel_to_snake }}(inputs), expected] + +{% endfor %} diff --git a/exercises/practice/difference-of-squares/.meta/tests.toml b/exercises/practice/difference-of-squares/.meta/tests.toml new file mode 100644 index 00000000..e54414c0 --- /dev/null +++ b/exercises/practice/difference-of-squares/.meta/tests.toml @@ -0,0 +1,37 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[e46c542b-31fc-4506-bcae-6b62b3268537] +description = "Square the sum of the numbers up to the given number -> square of sum 1" + +[9b3f96cb-638d-41ee-99b7-b4f9c0622948] +description = "Square the sum of the numbers up to the given number -> square of sum 5" + +[54ba043f-3c35-4d43-86ff-3a41625d5e86] +description = "Square the sum of the numbers up to the given number -> square of sum 100" + +[01d84507-b03e-4238-9395-dd61d03074b5] +description = "Sum the squares of the numbers up to the given number -> sum of squares 1" + +[c93900cd-8cc2-4ca4-917b-dd3027023499] +description = "Sum the squares of the numbers up to the given number -> sum of squares 5" + +[94807386-73e4-4d9e-8dec-69eb135b19e4] +description = "Sum the squares of the numbers up to the given number -> sum of squares 100" + +[44f72ae6-31a7-437f-858d-2c0837adabb6] +description = "Subtract sum of squares from square of sums -> difference of squares 1" + +[005cb2bf-a0c8-46f3-ae25-924029f8b00b] +description = "Subtract sum of squares from square of sums -> difference of squares 5" + +[b1bf19de-9a16-41c0-a62b-1f02ecc0b036] +description = "Subtract sum of squares from square of sums -> difference of squares 100" diff --git a/exercises/practice/difference-of-squares/difference_of_squares.gd b/exercises/practice/difference-of-squares/difference_of_squares.gd new file mode 100644 index 00000000..90d86374 --- /dev/null +++ b/exercises/practice/difference-of-squares/difference_of_squares.gd @@ -0,0 +1,10 @@ +func sum_of_squares(num: int) -> int: + pass + + +func square_of_sum(num: int) -> int: + pass + + +func difference_of_squares(num: int) -> int: + pass diff --git a/exercises/practice/difference-of-squares/difference_of_squares_test.gd b/exercises/practice/difference-of-squares/difference_of_squares_test.gd new file mode 100644 index 00000000..1f3707cb --- /dev/null +++ b/exercises/practice/difference-of-squares/difference_of_squares_test.gd @@ -0,0 +1,52 @@ +func test_square_of_sum_1(solution_script): + var inputs = 1 + var expected = 1 + return [solution_script.square_of_sum(inputs), expected] + + +func test_square_of_sum_5(solution_script): + var inputs = 5 + var expected = 225 + return [solution_script.square_of_sum(inputs), expected] + + +func test_square_of_sum_100(solution_script): + var inputs = 100 + var expected = 25502500 + return [solution_script.square_of_sum(inputs), expected] + + +func test_sum_of_squares_1(solution_script): + var inputs = 1 + var expected = 1 + return [solution_script.sum_of_squares(inputs), expected] + + +func test_sum_of_squares_5(solution_script): + var inputs = 5 + var expected = 55 + return [solution_script.sum_of_squares(inputs), expected] + + +func test_sum_of_squares_100(solution_script): + var inputs = 100 + var expected = 338350 + return [solution_script.sum_of_squares(inputs), expected] + + +func test_difference_of_squares_1(solution_script): + var inputs = 1 + var expected = 0 + return [solution_script.difference_of_squares(inputs), expected] + + +func test_difference_of_squares_5(solution_script): + var inputs = 5 + var expected = 170 + return [solution_script.difference_of_squares(inputs), expected] + + +func test_difference_of_squares_100(solution_script): + var inputs = 100 + var expected = 25164150 + return [solution_script.difference_of_squares(inputs), expected]