Skip to content

Commit cf487dd

Browse files
Trashtalk217jiegillet
authored andcommitted
Added Euclidean Algorithm for Lisp
1 parent 0128dfd commit cf487dd

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
;;;; Euclid algorithm implementation
2+
3+
(defun euclid-sub (a b)
4+
(euclid-sub* (abs a) (abs b)))
5+
6+
(defun euclid-sub* (a b)
7+
(if (eq a b)
8+
a
9+
(if (> a b)
10+
(euclid-sub* (- a b) b)
11+
(euclid-sub* a (- b a)))))
12+
13+
(defun euclid-mod (a b)
14+
(if (zerop b)
15+
(abs a)
16+
(euclid-mod b (mod a b))))
17+
18+
(print
19+
(euclid-sub (* 64 67)
20+
(* 64 81)))
21+
(print
22+
(euclid-mod (* 128 12)
23+
(* 128 77)))
24+
25+
;; built-in function: (gcd 80 40)
26+
;; quick test
27+
(assert
28+
(= (euclid-sub (* 64 67) (* 64 81))
29+
(gcd (* 64 67) (* 64 81))))
30+
31+
(assert
32+
(= (euclid-mod (* 64 67) (* 64 81))
33+
(gcd (* 64 67) (* 64 81))))

contents/euclidean_algorithm/euclidean_algorithm.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
1717
[import:3-16, lang="java"](code/java/EuclideanAlgo.java)
1818
{% sample lang="js" %}
1919
[import:15-29, lang="javascript"](code/javascript/euclidean_example.js)
20+
{% sample lang="lisp" %}
21+
[import:3-12, lang="lisp"](code/lisp/euclidean_algorithm.lisp)
2022
{% sample lang="py" %}
2123
[import:11-22, lang="python"](code/python/euclidean_example.py)
2224
{% sample lang="haskell" %}
@@ -60,6 +62,8 @@ Modern implementations, though, often use the modulus operator (%) like so
6062
[import:18-26, lang="java"](code/java/EuclideanAlgo.java)
6163
{% sample lang="js" %}
6264
[import:1-13, lang="javascript"](code/javascript/euclidean_example.js)
65+
{% sample lang="lisp" %}
66+
[import:13-17, lang="lisp"](code/lisp/euclidean_algorithm.lisp)
6367
{% sample lang="py" %}
6468
[import:1-9, lang="python"](code/python/euclidean_example.py)
6569
{% sample lang="haskell" %}
@@ -108,6 +112,8 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout
108112
[import, lang="java"](code/java/EuclideanAlgo.java)
109113
{% sample lang="js" %}
110114
[import, lang="javascript"](code/javascript/euclidean_example.js)
115+
{% sample lang="lisp" %}
116+
[import, lang="lisp"](code/lisp/euclidean_algorithm.lisp)
111117
{% sample lang="py" %}
112118
[import, lang="python"](code/python/euclidean_example.py)
113119
{% sample lang="haskell" %}

0 commit comments

Comments
 (0)