diff --git a/config.json b/config.json index 98859d1f..c7893fa3 100644 --- a/config.json +++ b/config.json @@ -485,6 +485,20 @@ "loops", "math" ] + }, + { + "slug": "nth-prime", + "name": "Nth Prime", + "uuid": "96b66be6-7e08-4ea6-8684-c21d3ce32900", + "practices": [], + "prerequisites": [], + "difficulty": 3, + "status": "active", + "topics": [ + "control_flow_conditionals", + "loops", + "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..f48d6d91 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": ["colinleach"], + "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/example.R b/exercises/practice/nth-prime/.meta/example.R new file mode 100644 index 00000000..f00101f0 --- /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 + repeat { + 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 + } +} 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..ccd0dfe3 --- /dev/null +++ b/exercises/practice/nth-prime/nth-prime.R @@ -0,0 +1,3 @@ +prime <- function(n) { + +} 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..3798a887 --- /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: nth-prime")