Skip to content

Commit

Permalink
feat: Add pythagorean-triplet (#171)
Browse files Browse the repository at this point in the history
* feat: Add pythagorean-triplet

* Remove comma from config.json

* pythagorean-triplet review feedback
  • Loading branch information
keiravillekode authored Nov 17, 2023
1 parent 2bf50e2 commit da81040
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "pythagorean-triplet",
"name": "Pythagorean Triplet",
"uuid": "915e4694-fb02-496e-9860-001d2aae70cd",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "pop-count",
"name": "Eliud's Eggs",
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": [
"pythagorean-triplet.v"
],
"test": [
"run_test.v"
],
"example": [
".meta/example.v"
]
},
"blurb": "There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product a * b * c.",
"source": "Problem 9 at Project Euler",
"source_url": "https://projecteuler.net/problem=9"
}
24 changes: 24 additions & 0 deletions exercises/practice/pythagorean-triplet/.meta/example.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module main

fn triplets_with_sum(n int) [][]int {
mut result := [][]int{}
if n < 2 {
return result
}
mut a := 0
for {
a++
numerator := n * (n - 2 * a)
denominator := 2 * (n - a)
b := numerator / denominator
if b < a {
break
}
if numerator % denominator != 0 {
continue
}
c := n - a - b
result << [a, b, c]
}
return result
}
24 changes: 24 additions & 0 deletions exercises/practice/pythagorean-triplet/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in 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"
4 changes: 4 additions & 0 deletions exercises/practice/pythagorean-triplet/pythagorean-triplet.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module main

fn triplets_with_sum(n int) [][]int {
}
60 changes: 60 additions & 0 deletions exercises/practice/pythagorean-triplet/run_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module main

fn test_triplets_whose_sum_is_12() {
expected := [
[3, 4, 5],
]
assert triplets_with_sum(12) == expected
}

fn test_triplets_whose_sum_is_108() {
expected := [
[27, 36, 45],
]
assert triplets_with_sum(108) == expected
}

fn test_triplets_whose_sum_is_1000() {
expected := [
[200, 375, 425],
]
assert triplets_with_sum(1000) == expected
}

fn test_no_matching_triplets_for_1001() {
expected := [][]int{}
assert triplets_with_sum(1001) == expected
}

fn test_returns_all_matching_triplets() {
expected := [
[9, 40, 41],
[15, 36, 39],
]
assert triplets_with_sum(90) == expected
}

fn test_several_matching_triplets() {
expected := [
[40, 399, 401],
[56, 390, 394],
[105, 360, 375],
[120, 350, 370],
[140, 336, 364],
[168, 315, 357],
[210, 280, 350],
[240, 252, 348],
]
assert triplets_with_sum(840) == expected
}

fn test_triplets_for_large_number() {
expected := [
[1200, 14375, 14425],
[1875, 14000, 14125],
[5000, 12000, 13000],
[6000, 11250, 12750],
[7500, 10000, 12500],
]
assert triplets_with_sum(30000) == expected
}

0 comments on commit da81040

Please sign in to comment.