Skip to content
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 @@ -366,6 +366,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "nth-prime",
"name": "Nth Prime",
"uuid": "a0dc033f-e760-4f34-abf4-850451044ba5",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/nth-prime/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
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": [
"glennj"
],
"files": {
"solution": [
"nth_prime.moon"
],
"test": [
"nth_prime_spec.moon"
],
"example": [
".meta/example.moon"
]
},
"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"
}
37 changes: 37 additions & 0 deletions exercises/practice/nth-prime/.meta/example.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- a coroutine wrapped up in a closure
prime_generator = ->
primes = {}

is_prime = (n) ->
sqrt = math.sqrt n
for p in *primes
return true if p > sqrt
return false if n % p == 0
true

coroutine.wrap ->
coroutine.yield 2
table.insert primes, 2
p = 3
while true
coroutine.yield p
table.insert primes, p
incrementing = true
while incrementing
p += 2
incrementing = not is_prime p


nth_prime = (n) ->
assert n > 0, 'there is no zeroth prime'

next_prime = prime_generator!
i = 0

while true
prime = next_prime!
i += 1
return prime if i == n


{ prime: nth_prime }
9 changes: 9 additions & 0 deletions exercises/practice/nth-prime/.meta/spec_generator.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
module_imports: {'prime'},

generate_test: (case, level) ->
if type(case.expected) == 'number'
indent "assert.are.equal #{case.expected}, prime #{case.input.number}", level
else
indent "assert.has.errors (-> prime #{case.input.number}), #{quote case.expected.error}", level
}
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"
4 changes: 4 additions & 0 deletions exercises/practice/nth-prime/nth_prime.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
prime: (n) ->
error 'Implement me'
}
17 changes: 17 additions & 0 deletions exercises/practice/nth-prime/nth_prime_spec.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import prime from require 'nth_prime'

describe 'nth-prime', ->
it 'first prime', ->
assert.are.equal 2, prime 1

pending 'second prime', ->
assert.are.equal 3, prime 2

pending 'sixth prime', ->
assert.are.equal 13, prime 6

pending 'big prime', ->
assert.are.equal 104743, prime 10001

pending 'there is no zeroth prime', ->
assert.has.errors (-> prime 0), 'there is no zeroth prime'
Loading