Skip to content

Commit cc0c887

Browse files
Add pythagorean-triplet exercise (#58)
1 parent a433a6e commit cc0c887

File tree

8 files changed

+151
-0
lines changed

8 files changed

+151
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@
390390
"prerequisites": [],
391391
"difficulty": 6
392392
},
393+
{
394+
"slug": "pythagorean-triplet",
395+
"name": "Pythagorean Triplet",
396+
"uuid": "5fbba379-44b1-445d-b2c2-7191e4706fae",
397+
"practices": [],
398+
"prerequisites": [],
399+
"difficulty": 6
400+
},
393401
{
394402
"slug": "book-store",
395403
"name": "Book Store",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Description
2+
3+
A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for which,
4+
5+
```text
6+
a² + b² = c²
7+
```
8+
9+
and such that,
10+
11+
```text
12+
a < b < c
13+
```
14+
15+
For example,
16+
17+
```text
18+
3² + 4² = 5².
19+
```
20+
21+
Given an input integer N, find all Pythagorean triplets for which `a + b + c = N`.
22+
23+
For example, with N = 1000, there is exactly one Pythagorean triplet for which `a + b + c = 1000`: `{200, 375, 425}`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Introduction
2+
3+
You are an accomplished problem-solver, known for your ability to tackle the most challenging mathematical puzzles.
4+
One evening, you receive an urgent letter from an inventor called the Triangle Tinkerer, who is working on a groundbreaking new project.
5+
The letter reads:
6+
7+
> Dear Mathematician,
8+
>
9+
> I need your help.
10+
> 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².
11+
> 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.
12+
> Calculating these triplets by hand would take me years, but I hear you are more than up to the task.
13+
>
14+
> Time is of the essence.
15+
> The future of my invention — and perhaps even the future of mathematical innovation — rests on your ability to solve this problem.
16+
17+
Motivated by the importance of the task, you set out to find all Pythagorean triplets that satisfy the condition.
18+
Your work could have far-reaching implications, unlocking new possibilities in science and engineering.
19+
Can you rise to the challenge and make history?
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"pythagorean_triplet.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
14+
]
15+
},
16+
"blurb": "Given an integer N, find all Pythagorean triplets for which a + b + c = N.",
17+
"source": "A variation of Problem 9 from Project Euler",
18+
"source_url": "https://projecteuler.net/problem=9"
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
let triplets_with_sum (n: i32): [][3]i32 =
2+
let (_, _, result) = loop (a, b, result) = (0, n / 2, []) while a < b do
3+
let a2 = a + 1
4+
let numerator = n * (n - 2 * a2)
5+
let denominator = 2 * (n - a2)
6+
let b2 = numerator / denominator
7+
in
8+
if a2 >= b2 || numerator % denominator != 0 then (a2, b2, result) else
9+
(a2, b2, result ++ [[a2, b2, n - a2 - b2]])
10+
in
11+
result
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[a19de65d-35b8-4480-b1af-371d9541e706]
13+
description = "triplets whose sum is 12"
14+
15+
[48b21332-0a3d-43b2-9a52-90b2a6e5c9f5]
16+
description = "triplets whose sum is 108"
17+
18+
[dffc1266-418e-4daa-81af-54c3e95c3bb5]
19+
description = "triplets whose sum is 1000"
20+
21+
[5f86a2d4-6383-4cce-93a5-e4489e79b186]
22+
description = "no matching triplets for 1001"
23+
24+
[bf17ba80-1596-409a-bb13-343bdb3b2904]
25+
description = "returns all matching triplets"
26+
27+
[9d8fb5d5-6c6f-42df-9f95-d3165963ac57]
28+
description = "several matching triplets"
29+
30+
[f5be5734-8aa0-4bd1-99a2-02adcc4402b4]
31+
description = "triplets for large number"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let triplets_with_sum (n: i32): [][3]i32 = ???
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import "pythagorean_triplet"
2+
3+
-- triplets whose sum is 12
4+
-- ==
5+
-- input { 12 }
6+
-- output { [[3, 4, 5]] }
7+
8+
-- triplets whose sum is 108
9+
-- ==
10+
-- input { 108 }
11+
-- output { [[27, 36, 45]] }
12+
13+
-- triplets whose sum is 1000
14+
-- ==
15+
-- input { 1000 }
16+
-- output { [[200, 375, 425]] }
17+
18+
-- no matching triplets for 1001
19+
-- ==
20+
-- input { 1001 }
21+
-- output { empty([0][3]i32) }
22+
23+
-- returns all matching triplets
24+
-- ==
25+
-- input { 90 }
26+
-- output { [[9, 40, 41], [15, 36, 39]] }
27+
28+
-- several matching triplets
29+
-- ==
30+
-- input { 840 }
31+
-- output { [[40, 399, 401], [56, 390, 394], [105, 360, 375], [120, 350, 370], [140, 336, 364], [168, 315, 357], [210, 280, 350], [240, 252, 348]] }
32+
33+
-- triplets for large number
34+
-- ==
35+
-- input { 30000 }
36+
-- output { [[1200, 14375, 14425], [1875, 14000, 14125], [5000, 12000, 13000], [6000, 11250, 12750], [7500, 10000, 12500]] }
37+
38+
let main (n: i32): [][3]i32 =
39+
triplets_with_sum n

0 commit comments

Comments
 (0)