Skip to content

Commit 0b8cb1b

Browse files
committed
Add q67
1 parent 5e8960c commit 0b8cb1b

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

src/ex/q066.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
(some #(when (= 0 (rem x %) (rem y %)) %) (range (min x y) 0 -1)))
99

1010
(defn ans2
11-
"Problem 66, using Euclid's algorithm (https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclid's_algorithm)"
11+
"Problem 66, using Euclid's algorithm,
12+
https://en.wikipedia.org/wiki/Greatest_common_divisor#Euclid's_algorithm"
1213
[x y]
1314
(loop [x x y y]
1415
(if (= x y) x (recur (- (max x y) (min x y)) (min x y)))))

src/ex/q067.clj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
;; https://4clojure.oxal.org/#/problem/067
2+
3+
(ns ex.q067)
4+
5+
(defn ans
6+
"Problem 67, Prime Numbers"
7+
[y]
8+
(letfn [(prime? [x]
9+
(cond
10+
(< x 2) false
11+
(= x 2) true
12+
(even? x) false
13+
:else (not-any? zero? (map #(mod x %) (range 3 (inc (Math/sqrt x)) 2)))))]
14+
(take y (filter prime? (range)))))

test/ex/q067_test.clj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
;; https://4clojure.oxal.org/#/problem/067
2+
3+
(ns ex.q067-test
4+
(:require [clojure.test :as t]
5+
[ex.q067 :as sut]))
6+
7+
(t/deftest test-q67
8+
(t/testing "Problem 67, Prime Numbers"
9+
(t/is (= [2 3] (sut/ans 2)))
10+
(t/is (= [2 3 5 7 11] (sut/ans 5)))
11+
(t/is (= 541 (last (sut/ans 100))))))

0 commit comments

Comments
 (0)