-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
5 changed files
with
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
^\.github$ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ |
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 @@ | ||
#' The Euclidean algorithm | ||
#' | ||
#' The algorithm takes two numeric scalars and applies the Euclidean | ||
#' algorithm to it. In the case these two numeric scalars are | ||
#' integers the greatest common divisor is yielded. | ||
#' | ||
#' A description with pseudocode can be found at the Wikipedia page here: | ||
#' https://en.wikipedia.org/wiki/Euclidean_algorithm. | ||
|
||
#' | ||
#' @param x A numeric scalar | ||
#' @param y A numeric scalar | ||
#' @return The Euclidean algorithm on x and y. For the case of two integers, the GCD is yielded. | ||
|
||
|
||
euclidean <- function(x,y){ | ||
# Stop if any argument isn't a numeric scalar | ||
stopifnot(is.numeric(x), length(x) == 1) | ||
stopifnot(is.numeric(y), length(y) == 1) | ||
|
||
if (y == 0){x} | ||
else {euclidean(y,x %% y)} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
test_that("GDC is calculated correctly.", { | ||
expect_equal(euclidean(123612, 13892347912), 4) | ||
expect_equal(euclidean(100, 1000), 100) | ||
expect_equal(euclidean(-100, 1000), 100) | ||
}) | ||
|
||
|
||
test_that("Wrong input throws an error.", { | ||
expect_error(euclidean("100", 1000)) | ||
expect_error(euclidean(100, "1000")) | ||
expect_error(euclidean(TRUE, "1000")) | ||
}) |
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