Skip to content

Commit

Permalink
Add nth-prime exercise (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored Oct 18, 2024
1 parent 7bb5519 commit 4fb9aec
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "nth-prime",
"name": "Nth Prime",
"uuid": "e61c707d-eb0d-4c37-8c0f-014db4504d3d",
"practices": [],
"prerequisites": [],
"difficulty": 5
}
]
},
Expand Down
7 changes: 7 additions & 0 deletions exercises/practice/nth-prime/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions exercises/practice/nth-prime/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
23 changes: 23 additions & 0 deletions exercises/practice/nth-prime/.meta/example.R
Original file line number Diff line number Diff line change
@@ -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]
}
25 changes: 25 additions & 0 deletions exercises/practice/nth-prime/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions exercises/practice/nth-prime/nth-prime.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
prime <- function(n) {

}
22 changes: 22 additions & 0 deletions exercises/practice/nth-prime/test_nth-prime.R
Original file line number Diff line number Diff line change
@@ -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))
})

0 comments on commit 4fb9aec

Please sign in to comment.