From 83a3b177defa49a01575adf7493149b3d9402087 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Mon, 8 May 2023 13:42:29 +0200 Subject: [PATCH] Add nth-prime exercise (#133) --- config.json | 12 +++++++ .../practice/nth-prime/.docs/instructions.md | 7 ++++ .../practice/nth-prime/.meta/config.json | 19 +++++++++++ .../practice/nth-prime/.meta/src/example.cljs | 33 +++++++++++++++++++ exercises/practice/nth-prime/.meta/tests.toml | 18 ++++++++++ exercises/practice/nth-prime/deps.edn | 10 ++++++ .../practice/nth-prime/src/nth_prime.cljs | 7 ++++ .../nth-prime/test/nth_prime_test.cljs | 23 +++++++++++++ 8 files changed, 129 insertions(+) create mode 100644 exercises/practice/nth-prime/.docs/instructions.md create mode 100644 exercises/practice/nth-prime/.meta/config.json create mode 100644 exercises/practice/nth-prime/.meta/src/example.cljs create mode 100644 exercises/practice/nth-prime/.meta/tests.toml create mode 100644 exercises/practice/nth-prime/deps.edn create mode 100644 exercises/practice/nth-prime/src/nth_prime.cljs create mode 100644 exercises/practice/nth-prime/test/nth_prime_test.cljs diff --git a/config.json b/config.json index 5b1a4cd8..643114f2 100644 --- a/config.json +++ b/config.json @@ -585,6 +585,18 @@ ], "difficulty": 2 }, + { + "slug": "nth-prime", + "name": "Nth Prime", + "uuid": "c5121449-5340-403f-8eff-f8c46db467ff", + "practices": [ + "numbers" + ], + "prerequisites": [ + "numbers" + ], + "difficulty": 2 + }, { "slug": "robot-name", "name": "Robot Name", diff --git a/exercises/practice/nth-prime/.docs/instructions.md b/exercises/practice/nth-prime/.docs/instructions.md new file mode 100644 index 00000000..065e323a --- /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/config.json b/exercises/practice/nth-prime/.meta/config.json new file mode 100644 index 00000000..d69fb234 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "ErikSchierboom" + ], + "files": { + "solution": [ + "src/nth_prime.cljs" + ], + "test": [ + "test/nth_prime_test.cljs" + ], + "example": [ + ".meta/src/example.cljs" + ] + }, + "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/src/example.cljs b/exercises/practice/nth-prime/.meta/src/example.cljs new file mode 100644 index 00000000..8e59e3c2 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/src/example.cljs @@ -0,0 +1,33 @@ +(ns nth-prime) + +(defn sqrt + "Wrapper around java's sqrt method." + [number] + (int (Math/ceil (Math/sqrt number)))) + +(defn divides? + "Helper function to decide if a number is evenly divided by divisor." + [number divisor] + (zero? (mod number divisor))) + +(defn- prime-by-trial-division? + "Simple trial division prime check." + [number] + (empty? (for [n (range 3 (inc (sqrt number)) 2) :when (divides? number n)] n))) + +(defn prime? [number] + (or (= 2 number) + (and (odd? number) (prime-by-trial-division? number)))) + +(defn next-prime [start] + (loop [n (inc start)] + (if (prime? n) + n + (recur (inc n))))) + +(def primes (iterate next-prime 1)) + +(defn nth-prime [index] + (when-not (pos? index) + (throw (js/Error. "nth-prime expects a positive integer for an argument"))) + (nth primes index)) diff --git a/exercises/practice/nth-prime/.meta/tests.toml b/exercises/practice/nth-prime/.meta/tests.toml new file mode 100644 index 00000000..a3af8598 --- /dev/null +++ b/exercises/practice/nth-prime/.meta/tests.toml @@ -0,0 +1,18 @@ +# This is an auto-generated file. Regular comments will be removed when this +# file is regenerated. Regenerating will not touch any manually added keys, +# so comments can be added in 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/deps.edn b/exercises/practice/nth-prime/deps.edn new file mode 100644 index 00000000..5c65da55 --- /dev/null +++ b/exercises/practice/nth-prime/deps.edn @@ -0,0 +1,10 @@ +{:deps + {org.clojure/clojure {:mvn/version "1.10.1"} + org.clojure/clojurescript {:mvn/version "1.10.773"}} + + :aliases + {:test + {:extra-paths ["test"] + :extra-deps + {olical/cljs-test-runner {:mvn/version "3.8.0"}} + :main-opts ["-m" "cljs-test-runner.main"]}}} diff --git a/exercises/practice/nth-prime/src/nth_prime.cljs b/exercises/practice/nth-prime/src/nth_prime.cljs new file mode 100644 index 00000000..67e10e58 --- /dev/null +++ b/exercises/practice/nth-prime/src/nth_prime.cljs @@ -0,0 +1,7 @@ +(ns nth-prime) + +(defn nth-prime + "Returns the prime number in the nth position." + [n] ;; <- arglist goes here + ;; your code goes here + ) diff --git a/exercises/practice/nth-prime/test/nth_prime_test.cljs b/exercises/practice/nth-prime/test/nth_prime_test.cljs new file mode 100644 index 00000000..9be2a47a --- /dev/null +++ b/exercises/practice/nth-prime/test/nth_prime_test.cljs @@ -0,0 +1,23 @@ +(ns nth-prime-test + (:require [clojure.test :refer [deftest testing is]] + nth-prime)) + +(deftest first-prime + (testing "the first prime is 2" + (is (= 2 (nth-prime/nth-prime 1))))) + +(deftest second-prime + (testing "the second prime is 3" + (is (= 3 (nth-prime/nth-prime 2))))) + +(deftest sixth-prime + (testing "the sixth prime is 13" + (is (= 13 (nth-prime/nth-prime 6))))) + +(deftest ten-thousand-first-prime + (testing "the ten thousand and first prime is 104743" + (is (= 104743 (nth-prime/nth-prime 10001))))) + +(deftest zeroth-prime + (testing "there is no zeroth prime" + (is (thrown? js/Error (nth-prime/nth-prime 0)))))