Skip to content

Commit

Permalink
Add pythagorean-triplet exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom committed Dec 19, 2024
1 parent e979689 commit 029848a
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "pythagorean-triplet",
"name": "Pythagorean Triplet",
"uuid": "e8e19e09-01b2-462c-abe5-b124f740b4f0",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "yacht",
"name": "Yacht",
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 @@
# 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}`.
19 changes: 19 additions & 0 deletions exercises/practice/pythagorean-triplet/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -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?
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": [
"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"
}
6 changes: 6 additions & 0 deletions exercises/practice/pythagorean-triplet/.meta/example.ua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
TripletsWithSum ← ▽= ⟜(
⧅<2 +1⇡
⊸(√≡/+×.)
≡⊂: ∩▽,⤙=⊸⌊
⊸≡/+
)
32 changes: 32 additions & 0 deletions exercises/practice/pythagorean-triplet/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions exercises/practice/pythagorean-triplet/pythagorean-triplet.ua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Find the Pythagorean triplet for a given sum
# Triplets ? Sum
TripletsWithSum ← |1 ⊙(⍤"Please implement TripletsWithSum" 0)
19 changes: 19 additions & 0 deletions exercises/practice/pythagorean-triplet/tests.ua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 029848a

Please sign in to comment.