Skip to content

Commit

Permalink
Add pythagorean-triplet exercise (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Jun 25, 2024
1 parent 2ed4818 commit 9c4d330
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions exercises/practice/pythagorean-triplet/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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}`.
19 changes: 19 additions & 0 deletions exercises/practice/pythagorean-triplet/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
31 changes: 31 additions & 0 deletions exercises/practice/pythagorean-triplet/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 2 additions & 0 deletions exercises/practice/pythagorean-triplet/dub.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name "pythagorean-triplet"
buildRequirements "disallowDeprecations"
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 9c4d330

Please sign in to comment.