From 2506982504f1cef51b8e4521b8908977c1751459 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Tue, 10 Dec 2024 09:19:18 +0100 Subject: [PATCH] Add `pythagorean-triplet` exercise --- config.json | 8 +++++ .../pythagorean-triplet/.docs/instructions.md | 23 +++++++++++++ .../pythagorean-triplet/.docs/introduction.md | 19 +++++++++++ .../pythagorean-triplet/.meta/config.json | 19 +++++++++++ .../pythagorean-triplet/.meta/example.ua | 6 ++++ .../pythagorean-triplet/.meta/tests.toml | 32 +++++++++++++++++++ .../pythagorean-triplet.ua | 3 ++ .../practice/pythagorean-triplet/tests.ua | 19 +++++++++++ 8 files changed, 129 insertions(+) create mode 100644 exercises/practice/pythagorean-triplet/.docs/instructions.md create mode 100644 exercises/practice/pythagorean-triplet/.docs/introduction.md create mode 100644 exercises/practice/pythagorean-triplet/.meta/config.json create mode 100644 exercises/practice/pythagorean-triplet/.meta/example.ua create mode 100644 exercises/practice/pythagorean-triplet/.meta/tests.toml create mode 100644 exercises/practice/pythagorean-triplet/pythagorean-triplet.ua create mode 100644 exercises/practice/pythagorean-triplet/tests.ua diff --git a/config.json b/config.json index 85a074a..9ba8212 100644 --- a/config.json +++ b/config.json @@ -498,6 +498,14 @@ "prerequisites": [], "difficulty": 5 }, + { + "slug": "pythagorean-triplet", + "name": "Pythagorean Triplet", + "uuid": "e8e19e09-01b2-462c-abe5-b124f740b4f0", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "yacht", "name": "Yacht", diff --git a/exercises/practice/pythagorean-triplet/.docs/instructions.md b/exercises/practice/pythagorean-triplet/.docs/instructions.md new file mode 100644 index 0000000..ced833d --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.docs/instructions.md @@ -0,0 +1,23 @@ +# Description + +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/.docs/introduction.md b/exercises/practice/pythagorean-triplet/.docs/introduction.md new file mode 100644 index 0000000..3453c6e --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.docs/introduction.md @@ -0,0 +1,19 @@ +# Introduction + +You are an accomplished problem-solver, known for your ability to tackle the most challenging mathematical puzzles. +One evening, you receive an urgent letter from an inventor called the Triangle Tinkerer, who is working on a groundbreaking new project. +The letter reads: + +> Dear Mathematician, +> +> I need your help. +> I am designing a device that relies on the unique properties of Pythagorean triplets — sets of three integers that satisfy the equation a² + b² = c². +> This device will revolutionize navigation, but for it to work, I must program it with every possible triplet where the sum of a, b, and c equals a specific number, N. +> Calculating these triplets by hand would take me years, but I hear you are more than up to the task. +> +> Time is of the essence. +> The future of my invention — and perhaps even the future of mathematical innovation — rests on your ability to solve this problem. + +Motivated by the importance of the task, you set out to find all Pythagorean triplets that satisfy the condition. +Your work could have far-reaching implications, unlocking new possibilities in science and engineering. +Can you rise to the challenge and make history? diff --git a/exercises/practice/pythagorean-triplet/.meta/config.json b/exercises/practice/pythagorean-triplet/.meta/config.json new file mode 100644 index 0000000..1fe55b9 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "erikschierboom" + ], + "files": { + "solution": [ + "pythagorean-triplet.ua" + ], + "test": [ + "tests.ua" + ], + "example": [ + ".meta/example.ua" + ] + }, + "blurb": "Given an integer N, find all Pythagorean triplets for which a + b + c = N.", + "source": "A variation of Problem 9 from Project Euler", + "source_url": "https://projecteuler.net/problem=9" +} diff --git a/exercises/practice/pythagorean-triplet/.meta/example.ua b/exercises/practice/pythagorean-triplet/.meta/example.ua new file mode 100644 index 0000000..6189dc5 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.meta/example.ua @@ -0,0 +1,6 @@ +TripletsWithSum ← ▽= ⟜( + ⧅<2 +1⇡ + ⊸(√≡/+×.) + ≡⊂: ∩▽,⤙=⊸⌊ + ⊸≡/+ +) diff --git a/exercises/practice/pythagorean-triplet/.meta/tests.toml b/exercises/practice/pythagorean-triplet/.meta/tests.toml new file mode 100644 index 0000000..56b01db --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.meta/tests.toml @@ -0,0 +1,32 @@ +# 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" +include = false diff --git a/exercises/practice/pythagorean-triplet/pythagorean-triplet.ua b/exercises/practice/pythagorean-triplet/pythagorean-triplet.ua new file mode 100644 index 0000000..73f352f --- /dev/null +++ b/exercises/practice/pythagorean-triplet/pythagorean-triplet.ua @@ -0,0 +1,3 @@ +# Find the Pythagorean triplet for a given sum +# Triplets ? Sum +TripletsWithSum ← |1 ⊙(⍤"Please implement TripletsWithSum" 0) diff --git a/exercises/practice/pythagorean-triplet/tests.ua b/exercises/practice/pythagorean-triplet/tests.ua new file mode 100644 index 0000000..26ddf96 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/tests.ua @@ -0,0 +1,19 @@ +~ "pythagorean-triplet.ua" ~ TripletsWithSum + +# Triplets whose sum is 12 +⍤⤙≍ [3_4_5] TripletsWithSum 12 + +# Triplets whose sum is 108 +⍤⤙≍ [27_36_45] TripletsWithSum 108 + +# Triplets whose sum is 1000 +⍤⤙≍ [200_375_425] TripletsWithSum 1000 + +# No matching triplets for 1001 +⍤⤙≍ ↯0_3[] TripletsWithSum 1001 + +# Returns all matching triplets +⍤⤙≍ [9_40_41 15_36_39] TripletsWithSum 90 + +# Several matching triplets +⍤⤙≍ [40_399_401 56_390_394 105_360_375 120_350_370 140_336_364 168_315_357 210_280_350 240_252_348] TripletsWithSum 840