From 423ceb18f4c46e7597c5f7462b97bc5c28e380ca Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 17 Apr 2023 18:10:21 -0700 Subject: [PATCH 1/6] half-done nth-prime example --- config.json | 12 +++++++ .../practice/nth-prime/.docs/instructions.md | 7 ++++ .../practice/nth-prime/.meta/config.json | 17 ++++++++++ exercises/practice/nth-prime/.meta/tests.toml | 25 ++++++++++++++ exercises/practice/nth-prime/nth-prime.R | 33 +++++++++++++++++++ exercises/practice/nth-prime/test_nth-prime.R | 32 ++++++++++++++++++ 6 files changed, 126 insertions(+) create mode 100644 exercises/practice/nth-prime/.docs/instructions.md create mode 100644 exercises/practice/nth-prime/.meta/config.json create mode 100644 exercises/practice/nth-prime/.meta/tests.toml create mode 100644 exercises/practice/nth-prime/nth-prime.R create mode 100644 exercises/practice/nth-prime/test_nth-prime.R diff --git a/config.json b/config.json index 98859d1f..0f0b9777 100644 --- a/config.json +++ b/config.json @@ -485,6 +485,18 @@ "loops", "math" ] + }, + { + "slug": "nth-prime", + "name": "Nth Prime", + "uuid": "96b66be6-7e08-4ea6-8684-c21d3ce32900", + "practices": [], + "prerequisites": [], + "difficulty": 1, + "status": "wip", + "topics": [ + "math" + ] } ] }, diff --git a/exercises/practice/nth-prime/.docs/instructions.md b/exercises/practice/nth-prime/.docs/instructions.md new file mode 100644 index 00000000..065e323a --- /dev/null +++ b/exercises/practice/nth-prime/.docs/instructions.md @@ -0,0 +1,7 @@ +# Instructions + +Given a number n, determine what the nth prime is. + +By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. + +If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself. diff --git a/exercises/practice/nth-prime/.meta/config.json b/exercises/practice/nth-prime/.meta/config.json new file mode 100644 index 00000000..d84dfd7e --- /dev/null +++ b/exercises/practice/nth-prime/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": [], + "files": { + "solution": [ + "nth-prime.R" + ], + "test": [ + "test_nth-prime.R" + ], + "example": [ + ".meta/example.R" + ] + }, + "blurb": "Given a number n, determine what the nth prime is.", + "source": "A variation on Problem 7 at Project Euler", + "source_url": "https://projecteuler.net/problem=7" +} diff --git a/exercises/practice/nth-prime/.meta/tests.toml b/exercises/practice/nth-prime/.meta/tests.toml new file mode 100644 index 00000000..daccec42 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/tests.toml @@ -0,0 +1,25 @@ +# 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. + +[75c65189-8aef-471a-81de-0a90c728160c] +description = "first prime" + +[2c38804c-295f-4701-b728-56dea34fd1a0] +description = "second prime" + +[56692534-781e-4e8c-b1f9-3e82c1640259] +description = "sixth prime" + +[fce1e979-0edb-412d-93aa-2c744e8f50ff] +description = "big prime" + +[bd0a9eae-6df7-485b-a144-80e13c7d55b2] +description = "there is no zeroth prime" diff --git a/exercises/practice/nth-prime/nth-prime.R b/exercises/practice/nth-prime/nth-prime.R new file mode 100644 index 00000000..1431ed67 --- /dev/null +++ b/exercises/practice/nth-prime/nth-prime.R @@ -0,0 +1,33 @@ + +prime <- function(n) { + if (n < 1) stop("n must be a positive integer") + + known <- vector("integer", n) + is_prime <- function(candidate) { + sqrt_candidate <- sqrt(candidate) + for (item in known) { + if (item > sqrt_candidate) return(TRUE) + if (candidate %% item == 0) return(FALSE) + TRUE + } + } + + known[1] <- 2 + known[2] <- 3 + known_count <- 2 + number <- 6 + while (TRUE) { + if (known_count >= n) return(known[n]) + if (is_prime(number - 1)) { + known_count <- known_count + 1 + known[known_count] <- number - 1 + } + if (known_count >= n) return(known[n]) + if (is_prime(number + 1)) { + known_count <- known_count + 1 + known[known_count] <- number + 1 + } + number <- number + 6 + } + +} \ No newline at end of file diff --git a/exercises/practice/nth-prime/test_nth-prime.R b/exercises/practice/nth-prime/test_nth-prime.R new file mode 100644 index 00000000..991f99d0 --- /dev/null +++ b/exercises/practice/nth-prime/test_nth-prime.R @@ -0,0 +1,32 @@ +source("./nth-prime.R") +library(testthat) + +context("nth-prime") + +test_that("first prime", { + n <- 1 + expect_equal(prime(n), 2) +}) + +test_that("second prime", { + n <- 2 + expect_equal(prime(n), 3) +}) + +test_that("sixth prime", { + n <- 6 + expect_equal(prime(n), 13) +}) + +test_that("big prime", { + n <- 10001 + expect_equal(prime(n), 104743) +}) + +test_that("there is no zeroth prime", { + n <- 0 + expect_error(prime(n)) +}) + + +message("All tests passed for exercise: luhn") From 7ce5ff3d42adaff1564ab4a7e2844cc48918c0ef Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 17 Apr 2023 23:33:48 -0700 Subject: [PATCH 2/6] working example code --- exercises/practice/nth-prime/.meta/example.R | 35 ++++++++++++++++++++ exercises/practice/nth-prime/nth-prime.R | 34 ++----------------- 2 files changed, 37 insertions(+), 32 deletions(-) create mode 100644 exercises/practice/nth-prime/.meta/example.R diff --git a/exercises/practice/nth-prime/.meta/example.R b/exercises/practice/nth-prime/.meta/example.R new file mode 100644 index 00000000..a4df5351 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/example.R @@ -0,0 +1,35 @@ +# Not the prettiest code, but it passes the tests reasonably quickly + +prime <- function(n) { + if (n < 1) stop("n must be a positive integer") + + known <- vector("integer", n) # to store primes as we find them + + is_prime <- function(candidate) { + sqrt_candidate <- sqrt(candidate) + for (item in known) { + if (item > sqrt_candidate) return(TRUE) + if (candidate %% item == 0) return(FALSE) + TRUE + } + } + + # Early primes are added explicitly: + known[1] <- 2 + known[2] <- 3 + + # Prime candidates are 6i +/- 1 : 5, 7, 11, 13... + # Python does this with `itertools.choice()`. Is there an equivalent in R? + known_count <- 2 + number <- 6 + while (TRUE) { + for (candidate in c(number - 1, number + 1)) { + if (known_count >= n) return(known[n]) # result! + if (is_prime(candidate)) { + known_count <- known_count + 1 + known[known_count] <- candidate + } + } + number <- number + 6 + } +} \ No newline at end of file diff --git a/exercises/practice/nth-prime/nth-prime.R b/exercises/practice/nth-prime/nth-prime.R index 1431ed67..77640559 100644 --- a/exercises/practice/nth-prime/nth-prime.R +++ b/exercises/practice/nth-prime/nth-prime.R @@ -1,33 +1,3 @@ - prime <- function(n) { - if (n < 1) stop("n must be a positive integer") - - known <- vector("integer", n) - is_prime <- function(candidate) { - sqrt_candidate <- sqrt(candidate) - for (item in known) { - if (item > sqrt_candidate) return(TRUE) - if (candidate %% item == 0) return(FALSE) - TRUE - } - } - - known[1] <- 2 - known[2] <- 3 - known_count <- 2 - number <- 6 - while (TRUE) { - if (known_count >= n) return(known[n]) - if (is_prime(number - 1)) { - known_count <- known_count + 1 - known[known_count] <- number - 1 - } - if (known_count >= n) return(known[n]) - if (is_prime(number + 1)) { - known_count <- known_count + 1 - known[known_count] <- number + 1 - } - number <- number + 6 - } - -} \ No newline at end of file + + } \ No newline at end of file From 5f2ecb9c810806cc96216e245c3337b233109f9c Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 17 Apr 2023 23:39:37 -0700 Subject: [PATCH 3/6] minor tweaks --- config.json | 6 ++++-- exercises/practice/nth-prime/.meta/example.R | 2 +- exercises/practice/nth-prime/nth-prime.R | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/config.json b/config.json index 0f0b9777..022addcb 100644 --- a/config.json +++ b/config.json @@ -492,11 +492,13 @@ "uuid": "96b66be6-7e08-4ea6-8684-c21d3ce32900", "practices": [], "prerequisites": [], - "difficulty": 1, + "difficulty": 3, "status": "wip", "topics": [ + "control_flow_conditionals", + "loops", "math" - ] + ] } ] }, diff --git a/exercises/practice/nth-prime/.meta/example.R b/exercises/practice/nth-prime/.meta/example.R index a4df5351..e99f0e0b 100644 --- a/exercises/practice/nth-prime/.meta/example.R +++ b/exercises/practice/nth-prime/.meta/example.R @@ -32,4 +32,4 @@ prime <- function(n) { } number <- number + 6 } -} \ No newline at end of file +} diff --git a/exercises/practice/nth-prime/nth-prime.R b/exercises/practice/nth-prime/nth-prime.R index 77640559..ccd0dfe3 100644 --- a/exercises/practice/nth-prime/nth-prime.R +++ b/exercises/practice/nth-prime/nth-prime.R @@ -1,3 +1,3 @@ prime <- function(n) { - } \ No newline at end of file +} From aafe4f03c42ab4f64a4c4878f54fa53fec8c0bb3 Mon Sep 17 00:00:00 2001 From: colinleach Date: Mon, 17 Apr 2023 23:42:46 -0700 Subject: [PATCH 4/6] fixed typo --- exercises/practice/nth-prime/test_nth-prime.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/nth-prime/test_nth-prime.R b/exercises/practice/nth-prime/test_nth-prime.R index 991f99d0..3798a887 100644 --- a/exercises/practice/nth-prime/test_nth-prime.R +++ b/exercises/practice/nth-prime/test_nth-prime.R @@ -29,4 +29,4 @@ test_that("there is no zeroth prime", { }) -message("All tests passed for exercise: luhn") +message("All tests passed for exercise: nth-prime") From 6320bbd37cc8ba5427cab21d4b798ce473827338 Mon Sep 17 00:00:00 2001 From: colinleach Date: Tue, 18 Apr 2023 07:47:33 -0700 Subject: [PATCH 5/6] cleaned loop syntax, added author --- exercises/practice/nth-prime/.meta/config.json | 2 +- exercises/practice/nth-prime/.meta/example.R | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/nth-prime/.meta/config.json b/exercises/practice/nth-prime/.meta/config.json index d84dfd7e..f48d6d91 100644 --- a/exercises/practice/nth-prime/.meta/config.json +++ b/exercises/practice/nth-prime/.meta/config.json @@ -1,5 +1,5 @@ { - "authors": [], + "authors": ["colinleach"], "files": { "solution": [ "nth-prime.R" diff --git a/exercises/practice/nth-prime/.meta/example.R b/exercises/practice/nth-prime/.meta/example.R index e99f0e0b..f00101f0 100644 --- a/exercises/practice/nth-prime/.meta/example.R +++ b/exercises/practice/nth-prime/.meta/example.R @@ -22,7 +22,7 @@ prime <- function(n) { # Python does this with `itertools.choice()`. Is there an equivalent in R? known_count <- 2 number <- 6 - while (TRUE) { + repeat { for (candidate in c(number - 1, number + 1)) { if (known_count >= n) return(known[n]) # result! if (is_prime(candidate)) { From 66ab08e627024b2208eb6f5cc9b7de0c44ea74e3 Mon Sep 17 00:00:00 2001 From: colinleach Date: Tue, 18 Apr 2023 08:07:10 -0700 Subject: [PATCH 6/6] status to active --- config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.json b/config.json index 022addcb..c7893fa3 100644 --- a/config.json +++ b/config.json @@ -493,7 +493,7 @@ "practices": [], "prerequisites": [], "difficulty": 3, - "status": "wip", + "status": "active", "topics": [ "control_flow_conditionals", "loops",