-
-
Notifications
You must be signed in to change notification settings - Fork 7
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 * Switch to a recursive implementation as suggested by Isaac
- Loading branch information
Showing
8 changed files
with
144 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,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" |
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": [ | ||
"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" | ||
} |
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,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 %} |
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,5 @@ | ||
module [prime] | ||
|
||
prime : U64 -> Result U64 _ | ||
prime = \number -> | ||
crash "Please implement the 'prime' 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,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 | ||
|