diff --git a/config.json b/config.json index 0081499..67c9f99 100644 --- a/config.json +++ b/config.json @@ -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", 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/Example.roc b/exercises/practice/nth-prime/.meta/Example.roc new file mode 100644 index 0000000..65ddf69 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/Example.roc @@ -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" diff --git a/exercises/practice/nth-prime/.meta/config.json b/exercises/practice/nth-prime/.meta/config.json new file mode 100644 index 0000000..a9fadd4 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/config.json @@ -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" +} diff --git a/exercises/practice/nth-prime/.meta/template.j2 b/exercises/practice/nth-prime/.meta/template.j2 new file mode 100644 index 0000000..cc0d133 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/template.j2 @@ -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 %} 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/NthPrime.roc b/exercises/practice/nth-prime/NthPrime.roc new file mode 100644 index 0000000..335618c --- /dev/null +++ b/exercises/practice/nth-prime/NthPrime.roc @@ -0,0 +1,5 @@ +module [prime] + +prime : U64 -> Result U64 _ +prime = \number -> + crash "Please implement the 'prime' function" diff --git a/exercises/practice/nth-prime/nth-prime-test.roc b/exercises/practice/nth-prime/nth-prime-test.roc new file mode 100644 index 0000000..6eb203a --- /dev/null +++ b/exercises/practice/nth-prime/nth-prime-test.roc @@ -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 +