This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
37ff8b1
commit 83a3b17
Showing
8 changed files
with
129 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,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" | ||
} |
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,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)) |
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,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" |
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,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"]}}} |
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 @@ | ||
(ns nth-prime) | ||
|
||
(defn nth-prime | ||
"Returns the prime number in the nth position." | ||
[n] ;; <- arglist goes here | ||
;; your code goes here | ||
) |
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,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))))) |