Skip to content

Commit

Permalink
Add q67
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Sep 11, 2024
1 parent 5e8960c commit 0b8cb1b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ex/q066.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
(some #(when (= 0 (rem x %) (rem y %)) %) (range (min x y) 0 -1)))

(defn ans2
"Problem 66, using Euclid's algorithm (https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclid's_algorithm)"
"Problem 66, using Euclid's algorithm,
https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclid's_algorithm"
[x y]
(loop [x x y y]
(if (= x y) x (recur (- (max x y) (min x y)) (min x y)))))
14 changes: 14 additions & 0 deletions src/ex/q067.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;; https://4clojure.oxal.org/#/problem/067

(ns ex.q067)

(defn ans
"Problem 67, Prime Numbers"
[y]
(letfn [(prime? [x]
(cond
(< x 2) false
(= x 2) true
(even? x) false
:else (not-any? zero? (map #(mod x %) (range 3 (inc (Math/sqrt x)) 2)))))]
(take y (filter prime? (range)))))
11 changes: 11 additions & 0 deletions test/ex/q067_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
;; https://4clojure.oxal.org/#/problem/067

(ns ex.q067-test
(:require [clojure.test :as t]
[ex.q067 :as sut]))

(t/deftest test-q67
(t/testing "Problem 67, Prime Numbers"
(t/is (= [2 3] (sut/ans 2)))
(t/is (= [2 3 5 7 11] (sut/ans 5)))
(t/is (= 541 (last (sut/ans 100))))))

0 comments on commit 0b8cb1b

Please sign in to comment.