Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions algorithms/math/Greatest-Common-Divisor.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns math.Greatest-Common-Divisor)

;; Given two integers, write a function which returns the greatest common divisor.

(defn GCM [x y]
(if (= y 0)
x
(GCM y (mod x y))))

;; (= (GCM 2 4) 2)
;; (= (GCM 10 5) 5)
;; (= (GCM 5 7) 1)
;; (= (GCM 1023 858) 33)
29 changes: 29 additions & 0 deletions algorithms/math/Least-Common-Multiple.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(ns math.Least-Common-Multiple)

;; Write a function which calculates the least common multiple. Your function should accept a variable number of positive integers or ratios.

(defn LCM [& arg]
((fn kpk [& data]
(let [data-first (nth data 0)
data-new (nth data 1)
normal-data (flatten data-new)
x (vec normal-data)]
(let [check (for [i (range (dec (count x)))]
(if (= (x i) (x (inc i)))
true
false))
lowest (apply min x)]
(if (some #(= false %) check)
(kpk data-first
(for [i (range (count x))]
(if (= (x i) lowest)
(+ (x i) (nth data-first i))
(x i))))
(x 0)))))
(vec arg) (vec arg)))

;; (== (LCM 2 3) 6)
;; (== (LCM 5 3 7) 105)
;; (== (LCM 1/3 2/5) 2)
;; (== (LCM 3/4 1/6) 3/2)
;; (== (LCM 7 5/7 2 3/5) 210)