-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add nth-prime exercise resolves #136 * Test NthPrime.prime * reformat nth-prime config.json
- Loading branch information
1 parent
6eb554a
commit d95acfc
Showing
9 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |