Skip to content

Commit

Permalink
Add another impl for q66
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Sep 9, 2024
1 parent 6636c53 commit 5e8960c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/ex/q066.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@
"Problem 66, Greatest Common Divisor"
[x y]
(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)"
[x y]
(loop [x x y y]
(if (= x y) x (recur (- (max x y) (min x y)) (min x y)))))
7 changes: 6 additions & 1 deletion test/ex/q066_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
(t/is (= 2 (sut/ans 2 4)))
(t/is (= 5 (sut/ans 10 5)))
(t/is (= 1 (sut/ans 5 7)))
(t/is (= 33 (sut/ans 1023 858)))))
(t/is (= 33 (sut/ans 1023 858)))
;; version 2
(t/is (= 2 (sut/ans2 2 4)))
(t/is (= 5 (sut/ans2 10 5)))
(t/is (= 1 (sut/ans2 5 7)))
(t/is (= 33 (sut/ans2 1023 858)))))

0 comments on commit 5e8960c

Please sign in to comment.