diff --git a/config.json b/config.json index fe38308..019778c 100644 --- a/config.json +++ b/config.json @@ -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", diff --git a/exercises/practice/nth-prime/.docs/instructions.md b/exercises/practice/nth-prime/.docs/instructions.md new file mode 100644 index 0000000..065e323 --- /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 0000000..6aa90cd --- /dev/null +++ b/exercises/practice/nth-prime/.meta/config.json @@ -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" +} diff --git a/exercises/practice/nth-prime/.meta/proof.ci.wren b/exercises/practice/nth-prime/.meta/proof.ci.wren new file mode 100644 index 0000000..cee81dc --- /dev/null +++ b/exercises/practice/nth-prime/.meta/proof.ci.wren @@ -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 + } +} diff --git a/exercises/practice/nth-prime/.meta/tests.toml b/exercises/practice/nth-prime/.meta/tests.toml new file mode 100644 index 0000000..daccec4 --- /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/LICENSE b/exercises/practice/nth-prime/LICENSE new file mode 100644 index 0000000..1b03cef --- /dev/null +++ b/exercises/practice/nth-prime/LICENSE @@ -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. diff --git a/exercises/practice/nth-prime/nth-prime.spec.wren b/exercises/practice/nth-prime/nth-prime.spec.wren new file mode 100644 index 0000000..29ef72f --- /dev/null +++ b/exercises/practice/nth-prime/nth-prime.spec.wren @@ -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") +} diff --git a/exercises/practice/nth-prime/nth-prime.wren b/exercises/practice/nth-prime/nth-prime.wren new file mode 100644 index 0000000..39cd074 --- /dev/null +++ b/exercises/practice/nth-prime/nth-prime.wren @@ -0,0 +1,5 @@ +class NthPrime { + static prime(number) { + Fiber.abort("Remove this statement and implement this function") + } +} diff --git a/exercises/practice/nth-prime/package.wren b/exercises/practice/nth-prime/package.wren new file mode 100644 index 0000000..7a0cd89 --- /dev/null +++ b/exercises/practice/nth-prime/package.wren @@ -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()