Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nth prime: add new practice exercise #245

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
]
},
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.
17 changes: 17 additions & 0 deletions exercises/practice/nth-prime/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
35 changes: 35 additions & 0 deletions exercises/practice/nth-prime/.meta/example.R
Original file line number Diff line number Diff line change
@@ -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
}
}
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) {

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