-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add pythagorean-triplet (#171)
* feat: Add pythagorean-triplet * Remove comma from config.json * pythagorean-triplet review feedback
1 parent
2bf50e2
commit da81040
Showing
7 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
exercises/practice/pythagorean-triplet/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module main | ||
|
||
fn triplets_with_sum(n int) [][]int { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |