Skip to content

Commit

Permalink
Add nth-prime exercise (#136)
Browse files Browse the repository at this point in the history
* Add nth-prime exercise
* Switch to a recursive implementation as suggested by Isaac
  • Loading branch information
ageron authored Oct 9, 2024
1 parent 7f8034b commit 1a7418e
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "nth-prime",
"name": "Nth Prime",
"uuid": "43dd9f02-130f-4947-8469-37229e819748",
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "pangram",
"name": "Pangram",
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.
26 changes: 26 additions & 0 deletions exercises/practice/nth-prime/.meta/Example.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module [prime]

prime : U64 -> Result U64 [NoPrime0]
prime = \number ->
if number == 0 then
Err NoPrime0
else if number == 1 then
Ok 2
else if number == 2 then
Ok 3
else
help = \primes, index ->
if List.len primes == number then
primes
else
nextIndex = index + 2
newPrimes =
if primes |> List.any \p -> nextIndex % p == 0 then
primes
else
primes |> List.append nextIndex
help newPrimes nextIndex
help [2, 3, 5] 5
|> List.last
|> Result.mapErr \_ ->
crash "Unreachable: list cannot be empty"
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": [
"ageron"
],
"files": {
"solution": [
"NthPrime.roc"
],
"test": [
"nth-prime-test.roc"
],
"example": [
".meta/Example.roc"
]
},
"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"
}
17 changes: 17 additions & 0 deletions exercises/practice/nth-prime/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{%- import "generator_macros.j2" as macros with context -%}
{{ macros.canonical_ref() }}
{{ macros.header() }}

import {{ exercise | to_pascal }} exposing [{{ cases[0]["property"] | to_camel }}]

{% for case in cases -%}
# {{ case["description"] }}
expect
result = {{ case["property"] | to_camel }} {{ case["input"]["number"] }}
{%- if case["expected"]["error"] %}
result |> Result.isErr
{%- else %}
result == Ok {{ case["expected"] }}
{%- endif %}

{% endfor %}
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"
5 changes: 5 additions & 0 deletions exercises/practice/nth-prime/NthPrime.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module [prime]

prime : U64 -> Result U64 _
prime = \number ->
crash "Please implement the 'prime' function"
37 changes: 37 additions & 0 deletions exercises/practice/nth-prime/nth-prime-test.roc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# These tests are auto-generated with test data from:
# https://github.com/exercism/problem-specifications/tree/main/exercises/nth-prime/canonical-data.json
# File last updated on 2024-10-07
app [main] {
pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br",
}

main =
Task.ok {}

import NthPrime exposing [prime]

# first prime
expect
result = prime 1
result == Ok 2

# second prime
expect
result = prime 2
result == Ok 3

# sixth prime
expect
result = prime 6
result == Ok 13

# big prime
expect
result = prime 10001
result == Ok 104743

# there is no zeroth prime
expect
result = prime 0
result |> Result.isErr

0 comments on commit 1a7418e

Please sign in to comment.