Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Add nth-prime exercise (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 8, 2023
1 parent 37ff8b1 commit 83a3b17
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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.
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": [
"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"
}
33 changes: 33 additions & 0 deletions exercises/practice/nth-prime/.meta/src/example.cljs
Original file line number Diff line number Diff line change
@@ -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))
18 changes: 18 additions & 0 deletions exercises/practice/nth-prime/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 10 additions & 0 deletions exercises/practice/nth-prime/deps.edn
Original file line number Diff line number Diff line change
@@ -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"]}}}
7 changes: 7 additions & 0 deletions exercises/practice/nth-prime/src/nth_prime.cljs
Original file line number Diff line number Diff line change
@@ -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
)
23 changes: 23 additions & 0 deletions exercises/practice/nth-prime/test/nth_prime_test.cljs
Original file line number Diff line number Diff line change
@@ -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)))))

0 comments on commit 83a3b17

Please sign in to comment.