From 00e102add42157f7bfc199d0c232aa4e050911b3 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Fri, 18 Oct 2024 09:58:59 +0200 Subject: [PATCH] Add `nth-prime` exercise --- config.json | 8 ++++++ .../practice/nth-prime/.docs/instructions.md | 7 ++++++ .../practice/nth-prime/.meta/config.json | 19 ++++++++++++++ exercises/practice/nth-prime/.meta/example.R | 23 +++++++++++++++++ exercises/practice/nth-prime/.meta/tests.toml | 25 +++++++++++++++++++ exercises/practice/nth-prime/nth-prime.R | 3 +++ exercises/practice/nth-prime/test_nth-prime.R | 22 ++++++++++++++++ 7 files changed, 107 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/example.R 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 2c990de2..0ea6909a 100644 --- a/config.json +++ b/config.json @@ -848,6 +848,14 @@ "practices": [], "prerequisites": [], "difficulty": 4 + }, + { + "slug": "nth-prime", + "name": "Nth Prime", + "uuid": "e61c707d-eb0d-4c37-8c0f-014db4504d3d", + "practices": [], + "prerequisites": [], + "difficulty": 5 } ] }, 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..76301631 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "erikschierboom" + ], + "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..18210734 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/example.R @@ -0,0 +1,23 @@ +prime <- function(n) { + stopifnot(n > 0) + + primes <- c(2, 3) + current <- 6 + + while (length(primes) < n) { + limit <- sqrt(current + 1) + prime_check <- primes[primes <= limit] + + if (all((current - 1) %% prime_check != 0)) { + primes <- c(primes, current - 1) + } + + if (all((current + 1) %% prime_check != 0)) { + primes <- c(primes, current + 1) + } + + current <- current + 6 + } + + primes[n] +} 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..8c8862d6 --- /dev/null +++ b/exercises/practice/nth-prime/test_nth-prime.R @@ -0,0 +1,22 @@ +source("./nth-prime.R") +library(testthat) + +test_that("First prime", { + expect_equal(prime(1), 2) +}) + +test_that("Second prime", { + expect_equal(prime(2), 3) +}) + +test_that("Sixth prime", { + expect_equal(prime(6), 13) +}) + +test_that("Big prime", { + expect_equal(prime(10001), 104743) +}) + +test_that("There is no zeroth prime", { + expect_error(prime(0)) +})