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

Add nth-prime exercise #137

Merged
merged 3 commits into from
Nov 1, 2023
Merged
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
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "nth-prime",
"name": "Nth Prime",
"uuid": "b904e358-d446-41b2-a168-0be20a4c9df8",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "robot-simulator",
"name": "Robot Simulator",
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": [
"keiravillekode"
],
"files": {
"solution": [
"nth-prime.wren"
],
"test": [
"nth-prime.spec.wren"
],
"example": [
".meta/proof.ci.wren"
]
},
"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"
}
43 changes: 43 additions & 0 deletions exercises/practice/nth-prime/.meta/proof.ci.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class NthPrime {
static prime(number) {
if (number == 0) {
Fiber.abort("there is no zeroth prime")
}

// Special case for the first two primes.
if (number <= 2) {
return number + 1
}
var remaining = number - 2

// candidate is never a multiple of 2 or 3.
var candidate = 5
var step = 2
while (true) {
if (isPrime(candidate)) {
remaining = remaining - 1
if (remaining == 0) {
return candidate
}
}
candidate = candidate + step
step = 6 - step
}
}

// candidate must not be a multiple of 2 or 3.
static isPrime(candidate) {
var limit = candidate.sqrt

var factor = 5
var step = 2
while (factor <= limit) {
if (candidate % factor == 0) {
return false
}
factor = factor + step
step = 6 - step
}
return true
}
}
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"
21 changes: 21 additions & 0 deletions exercises/practice/nth-prime/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
28 changes: 28 additions & 0 deletions exercises/practice/nth-prime/nth-prime.spec.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import "./nth-prime" for NthPrime
import "wren-testie/testie" for Testie, Expect

Testie.test("NthPrime.prime()") { |do, skip|
do.test("first prime") {
Expect.value(NthPrime.prime(1)).toEqual(2)
}

skip.test("second prime") {
Expect.value(NthPrime.prime(2)).toEqual(3)
}

skip.test("third prime") {
Expect.value(NthPrime.prime(3)).toEqual(5)
}

skip.test("sixth prime") {
Expect.value(NthPrime.prime(6)).toEqual(13)
}

skip.test("big prime") {
Expect.value(NthPrime.prime(10001)).toEqual(104743)
}

Expect.that {
NthPrime.prime(0)
}.abortsWith("there is no zeroth prime")
}
5 changes: 5 additions & 0 deletions exercises/practice/nth-prime/nth-prime.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class NthPrime {
static prime(number) {
Fiber.abort("Remove this statement and implement this function")
}
}
14 changes: 14 additions & 0 deletions exercises/practice/nth-prime/package.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "wren-package" for WrenPackage, Dependency
import "os" for Process

class Package is WrenPackage {
construct new() {}
name { "exercism/nth-prime" }
dependencies {
return [
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git")
]
}
}

Package.new().default()