From 5ae36af354a1aaa822ac3c2f7887c1dfb811b1ac Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Mon, 6 May 2024 22:56:47 +1000 Subject: [PATCH 1/2] Add pythagorean-triplet exercise --- config.json | 11 +++++ .../pythagorean-triplet/.docs/instructions.md | 23 +++++++++++ .../pythagorean-triplet/.meta/config.json | 19 +++++++++ .../pythagorean-triplet/.meta/example.el | 30 ++++++++++++++ .../pythagorean-triplet/.meta/tests.toml | 31 ++++++++++++++ .../pythagorean-triplet-test.el | 40 +++++++++++++++++++ .../pythagorean-triplet.el | 13 ++++++ 7 files changed, 167 insertions(+) create mode 100644 exercises/practice/pythagorean-triplet/.docs/instructions.md create mode 100644 exercises/practice/pythagorean-triplet/.meta/config.json create mode 100644 exercises/practice/pythagorean-triplet/.meta/example.el create mode 100644 exercises/practice/pythagorean-triplet/.meta/tests.toml create mode 100644 exercises/practice/pythagorean-triplet/pythagorean-triplet-test.el create mode 100644 exercises/practice/pythagorean-triplet/pythagorean-triplet.el diff --git a/config.json b/config.json index d46f9db4..3eacc562 100644 --- a/config.json +++ b/config.json @@ -565,6 +565,17 @@ "strings" ] }, + { + "slug": "pythagorean-triplet", + "name": "Pythagorean Triplet", + "uuid": "f33d976d-5189-4fcb-b0ac-42879a1067bf", + "practices": [], + "prerequisites": [], + "difficulty": 5, + "topics": [ + "math" + ] + }, { "slug": "rail-fence-cipher", "name": "Rail Fence Cipher", diff --git a/exercises/practice/pythagorean-triplet/.docs/instructions.md b/exercises/practice/pythagorean-triplet/.docs/instructions.md new file mode 100644 index 00000000..1c1a8aea --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.docs/instructions.md @@ -0,0 +1,23 @@ +# Instructions + +A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for which, + +```text +a² + b² = c² +``` + +and such that, + +```text +a < b < c +``` + +For example, + +```text +3² + 4² = 5². +``` + +Given an input integer N, find all Pythagorean triplets for which `a + b + c = N`. + +For example, with N = 1000, there is exactly one Pythagorean triplet for which `a + b + c = 1000`: `{200, 375, 425}`. diff --git a/exercises/practice/pythagorean-triplet/.meta/config.json b/exercises/practice/pythagorean-triplet/.meta/config.json new file mode 100644 index 00000000..ce368d16 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "pythagorean-triplet.el" + ], + "test": [ + "pythagorean-triplet-test.el" + ], + "example": [ + ".meta/example.el" + ] + }, + "blurb": "There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the triplet.", + "source": "Problem 9 at Project Euler", + "source_url": "https://projecteuler.net/problem=9" +} diff --git a/exercises/practice/pythagorean-triplet/.meta/example.el b/exercises/practice/pythagorean-triplet/.meta/example.el new file mode 100644 index 00000000..51f1ecb8 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.meta/example.el @@ -0,0 +1,30 @@ +;;; pythagorean-triplet.el --- Pythagorean Triplet (exercism) -*- lexical-binding: t; -*- + +;;; Commentary: + +; For every Pythagorean triplet with total a + b + c = n, +; a² + b² = c² +; <=> a² + b² = (n - a - b)², substituting c +; <=> 0 = n² - 2*n*a - 2*n*b + 2*a*b +; <=> (2*n - 2*a) b = (n² - 2*n*a) +; <=> b = (n² - 2*n*a) / (2*n - 2*a) + +;;; Code: + +(require 'cl-lib) + +(defun triplets-with-sum (n) + (cl-labels + ((recur (start) + (cl-loop for a from start + for numerator = (* n (- n a a)) + for denominator = (* 2 (- n a)) + for b = (/ numerator denominator) + always (< a b) + until (= 0 (% numerator denominator)) + finally return (cons (list a b (- n a b)) (recur (1+ a)))))) + (unless (< n 2) + (recur 1)))) + +(provide 'pythagorean-triplet) +;;; pythagorean-triplet.el ends here diff --git a/exercises/practice/pythagorean-triplet/.meta/tests.toml b/exercises/practice/pythagorean-triplet/.meta/tests.toml new file mode 100644 index 00000000..719620a9 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.meta/tests.toml @@ -0,0 +1,31 @@ +# 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. + +[a19de65d-35b8-4480-b1af-371d9541e706] +description = "triplets whose sum is 12" + +[48b21332-0a3d-43b2-9a52-90b2a6e5c9f5] +description = "triplets whose sum is 108" + +[dffc1266-418e-4daa-81af-54c3e95c3bb5] +description = "triplets whose sum is 1000" + +[5f86a2d4-6383-4cce-93a5-e4489e79b186] +description = "no matching triplets for 1001" + +[bf17ba80-1596-409a-bb13-343bdb3b2904] +description = "returns all matching triplets" + +[9d8fb5d5-6c6f-42df-9f95-d3165963ac57] +description = "several matching triplets" + +[f5be5734-8aa0-4bd1-99a2-02adcc4402b4] +description = "triplets for large number" diff --git a/exercises/practice/pythagorean-triplet/pythagorean-triplet-test.el b/exercises/practice/pythagorean-triplet/pythagorean-triplet-test.el new file mode 100644 index 00000000..bfa01a4b --- /dev/null +++ b/exercises/practice/pythagorean-triplet/pythagorean-triplet-test.el @@ -0,0 +1,40 @@ +;;; pythagorean-triplet-test.el --- Tests for Pythagorean Triplet (exercism) -*- lexical-binding: t; -*- + +;;; Commentary: + +;;; Code: + +(load-file "pythagorean-triplet.el") +(declare-function triplets-with-sum "pythagorean-triplet.el" (n)) + + +(ert-deftest triplets-whose-sum-is-12 () + (should (equal '((3 4 5)) (triplets-with-sum 12)))) + + +(ert-deftest triplets-whose-sum-is-108 () + (should (equal '((27 36 45)) (triplets-with-sum 108)))) + + +(ert-deftest triplets-whose-sum-is-1000 () + (should (equal '((200 375 425)) (triplets-with-sum 1000)))) + + +(ert-deftest no-matching-triplets-for-1001 () + (should (equal '() (triplets-with-sum 1001)))) + + +(ert-deftest returns-all-matching-triplets () + (should (equal '((9 40 41) (15 36 39)) (triplets-with-sum 90)))) + + +(ert-deftest several-matching-triplets () + (should (equal '((40 399 401) (56 390 394) (105 360 375) (120 350 370) (140 336 364) (168 315 357) (210 280 350) (240 252 348)) (triplets-with-sum 840)))) + + +(ert-deftest triplets-for-large-number () + (should (equal '((1200 14375 14425) (1875 14000 14125) (5000 12000 13000) (6000 11250 12750) (7500 10000 12500)) (triplets-with-sum 30000)))) + + +(provide 'pythagorean-triplet-test) +;;; pythagorean-triplet-test.el ends here diff --git a/exercises/practice/pythagorean-triplet/pythagorean-triplet.el b/exercises/practice/pythagorean-triplet/pythagorean-triplet.el new file mode 100644 index 00000000..19ad38d9 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/pythagorean-triplet.el @@ -0,0 +1,13 @@ +;;; pythagorean-triplet.el --- Pythagorean Triplet (exercism) -*- lexical-binding: t; -*- + +;;; Commentary: + +;;; Code: + + +(defun triplets-with-sum (n) + (error "Delete this S-Expression and write your own implementation")) + + +(provide 'pythagorean-triplet) +;;; pythagorean-triplet.el ends here From 818d7d7c3bb3d2da07b5714a592139a989de73c0 Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Wed, 8 May 2024 17:45:49 +1000 Subject: [PATCH 2/2] Reformat test cases --- .../pythagorean-triplet-test.el | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/exercises/practice/pythagorean-triplet/pythagorean-triplet-test.el b/exercises/practice/pythagorean-triplet/pythagorean-triplet-test.el index bfa01a4b..81dbebf8 100644 --- a/exercises/practice/pythagorean-triplet/pythagorean-triplet-test.el +++ b/exercises/practice/pythagorean-triplet/pythagorean-triplet-test.el @@ -29,11 +29,24 @@ (ert-deftest several-matching-triplets () - (should (equal '((40 399 401) (56 390 394) (105 360 375) (120 350 370) (140 336 364) (168 315 357) (210 280 350) (240 252 348)) (triplets-with-sum 840)))) + (should (equal '((40 399 401) + (56 390 394) + (105 360 375) + (120 350 370) + (140 336 364) + (168 315 357) + (210 280 350) + (240 252 348)) + (triplets-with-sum 840)))) (ert-deftest triplets-for-large-number () - (should (equal '((1200 14375 14425) (1875 14000 14125) (5000 12000 13000) (6000 11250 12750) (7500 10000 12500)) (triplets-with-sum 30000)))) + (should (equal '((1200 14375 14425) + (1875 14000 14125) + (5000 12000 13000) + (6000 11250 12750) + (7500 10000 12500)) + (triplets-with-sum 30000)))) (provide 'pythagorean-triplet-test)