-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8eea35e
commit 872df5b
Showing
7 changed files
with
149 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,17 @@ | ||
{ | ||
"authors": [], | ||
"files": { | ||
"solution": [ | ||
"pythagorean-triplet.R" | ||
], | ||
"test": [ | ||
"test_pythagorean-triplet.R" | ||
], | ||
"example": [ | ||
".meta/example.R" | ||
] | ||
}, | ||
"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" | ||
} |
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,12 @@ | ||
pythagorean_triplet <- function(n) { | ||
triplets <- list() | ||
for (a in 1:n) { | ||
for (b in a + 1:n) { | ||
c <- n - a - b | ||
if (b < c && a^2 + b^2 == c^2) { | ||
triplets <- append(triplets, list(c(a, b, c))) | ||
} | ||
} | ||
} | ||
triplets | ||
} |
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,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" |
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,3 @@ | ||
pythagorean_triplet <- function(n) { | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
exercises/practice/pythagorean-triplet/test_pythagorean-triplet.R
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,51 @@ | ||
source("./pythagorean-triplet.R") | ||
library(testthat) | ||
|
||
test_that("triplets whose sum is 12", { | ||
expect_equal(pythagorean_triplet(12), list(3:5)) | ||
}) | ||
|
||
test_that("triplets whose sum is 108", { | ||
expect_equal(pythagorean_triplet(108), list(c(27, 36, 45))) | ||
}) | ||
|
||
test_that("triplets whose sum is 1000", { | ||
expect_equal(pythagorean_triplet(1000), list(c(200, 375, 425))) | ||
}) | ||
|
||
test_that("no matching triplets for 1001", { | ||
expect_equal(pythagorean_triplet(1001), list()) | ||
}) | ||
|
||
test_that("returns all matching triplets", { | ||
expect_equal(pythagorean_triplet(90), list(c(9, 40, 41), c(15, 36, 39))) | ||
}) | ||
|
||
test_that("several matching triplets", { | ||
expect_equal( | ||
pythagorean_triplet(840), | ||
list( | ||
c(40, 399, 401), | ||
c(56, 390, 394), | ||
c(105, 360, 375), | ||
c(120, 350, 370), | ||
c(140, 336, 364), | ||
c(168, 315, 357), | ||
c(210, 280, 350), | ||
c(240, 252, 348) | ||
) | ||
) | ||
}) | ||
|
||
test_that("triplets for large number", { | ||
expect_equal( | ||
pythagorean_triplet(30000), | ||
list( | ||
c(1200, 14375, 14425), | ||
c(1875, 14000, 14125), | ||
c(5000, 12000, 13000), | ||
c(6000, 11250, 12750), | ||
c(7500, 10000, 12500) | ||
) | ||
) | ||
}) |