diff --git a/config.json b/config.json index 6b323b2..3b45913 100644 --- a/config.json +++ b/config.json @@ -554,6 +554,14 @@ "prerequisites": [], "difficulty": 5 }, + { + "slug": "pythagorean-triplet", + "name": "Pythagorean Triplet", + "uuid": "dc415515-ad72-41a3-8d42-a7d976618848", + "practices": [], + "prerequisites": [], + "difficulty": 5 + }, { "slug": "scrabble-score", "name": "Scrabble Score", diff --git a/exercises/practice/pythagorean-triplet/.docs/instructions.md b/exercises/practice/pythagorean-triplet/.docs/instructions.md new file mode 100644 index 0000000..1c1a8ae --- /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 0000000..04b741e --- /dev/null +++ b/exercises/practice/pythagorean-triplet/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "source/pythagorean_triplet.d" + ], + "test": [ + "source/pythagorean_triplet.d" + ], + "example": [ + "example/pythagorean_triplet.d" + ] + }, + "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/tests.toml b/exercises/practice/pythagorean-triplet/.meta/tests.toml new file mode 100644 index 0000000..719620a --- /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/dub.sdl b/exercises/practice/pythagorean-triplet/dub.sdl new file mode 100644 index 0000000..e3bd5f4 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/dub.sdl @@ -0,0 +1,2 @@ +name "pythagorean-triplet" +buildRequirements "disallowDeprecations" diff --git a/exercises/practice/pythagorean-triplet/example/pythagorean_triplet.d b/exercises/practice/pythagorean-triplet/example/pythagorean_triplet.d new file mode 100644 index 0000000..1aba2bc --- /dev/null +++ b/exercises/practice/pythagorean-triplet/example/pythagorean_triplet.d @@ -0,0 +1,41 @@ +module pythagorean_triplet; + +struct Triplet { + uint a; + uint b; + uint c; +} + +// 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) + +pure Triplet[] tripletsWithSum(uint n) +{ + Triplet[] result; + if (n < 2) + { + return result; + } + + for (uint a = 1;;++a) + { + uint numerator = n * (n - 2 * a); + uint denominator = 2 * (n - a); + uint b = numerator / denominator; + if (b <= a) + { + break; + } + if (numerator % denominator != 0) + { + continue; + } + uint c = n - a - b; + result ~= Triplet(a, b, c); + } + return result; +} diff --git a/exercises/practice/pythagorean-triplet/source/pythagorean_triplet.d b/exercises/practice/pythagorean-triplet/source/pythagorean_triplet.d new file mode 100644 index 0000000..8515936 --- /dev/null +++ b/exercises/practice/pythagorean-triplet/source/pythagorean_triplet.d @@ -0,0 +1,87 @@ +module pythagorean_triplet; + +struct Triplet { + uint a; + uint b; + uint c; +} + +pure Triplet[] tripletsWithSum(uint n) +{ + // implement this function +} + +unittest +{ + immutable int allTestsEnabled = 0; + + // Triplets whose sum is 12 + { + Triplet[] expected = [ + Triplet(3, 4, 5), + ]; + assert(tripletsWithSum(12) == expected); + } + + static if (allTestsEnabled) + { + // Triplets whose sum is 108 + { + Triplet[] expected = [ + Triplet(27, 36, 45), + ]; + assert(tripletsWithSum(108) == expected); + } + + // Triplets whose sum is 1000 + { + Triplet[] expected = [ + Triplet(200, 375, 425), + ]; + assert(tripletsWithSum(1000) == expected); + } + + // No matching triplets for 1001 + { + Triplet[] expected = [ + ]; + assert(tripletsWithSum(1001) == expected); + } + + // Returns all matching triplets + { + Triplet[] expected = [ + Triplet(9, 40, 41), + Triplet(15, 36, 39), + ]; + assert(tripletsWithSum(90) == expected); + } + + // Several matching triplets + { + Triplet[] expected = [ + Triplet(40, 399, 401), + Triplet(56, 390, 394), + Triplet(105, 360, 375), + Triplet(120, 350, 370), + Triplet(140, 336, 364), + Triplet(168, 315, 357), + Triplet(210, 280, 350), + Triplet(240, 252, 348), + ]; + assert(tripletsWithSum(840) == expected); + } + + // Triplets for large number + { + Triplet[] expected = [ + Triplet(1200, 14375, 14425), + Triplet(1875, 14000, 14125), + Triplet(5000, 12000, 13000), + Triplet(6000, 11250, 12750), + Triplet(7500, 10000, 12500), + ]; + assert(tripletsWithSum(30000) == expected); + } + } +}