From 5e8960cdbd0fb6884555e7b1e2b577ff08992378 Mon Sep 17 00:00:00 2001 From: David Kun Date: Mon, 9 Sep 2024 18:46:42 -0400 Subject: [PATCH] Add another impl for q66 --- src/ex/q066.clj | 6 ++++++ test/ex/q066_test.clj | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ex/q066.clj b/src/ex/q066.clj index 03a168f..52d2bf3 100644 --- a/src/ex/q066.clj +++ b/src/ex/q066.clj @@ -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))))) \ No newline at end of file diff --git a/test/ex/q066_test.clj b/test/ex/q066_test.clj index a9f98be..bb5ef42 100644 --- a/test/ex/q066_test.clj +++ b/test/ex/q066_test.clj @@ -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))))) \ No newline at end of file + (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))))) \ No newline at end of file