Skip to content

Commit afcaf4d

Browse files
Trashtalk217berquist
authored andcommitted
Minor changes to euclidean algorithm implementation in Common Lisp (#609)
1 parent 40cf050 commit afcaf4d

File tree

3 files changed

+33
-36
lines changed

3 files changed

+33
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
;;;; Euclidean algorithm implementation in Common Lisp
2+
3+
(defun euclid-sub (a b)
4+
"Finds the greatest common divsor for any two integers"
5+
(defun euclid-sub* (a b)
6+
"Finds the greatest common divisor for any two positive integers"
7+
(if (eql a b)
8+
a
9+
(if (> a b)
10+
(euclid-sub* (- a b) b)
11+
(euclid-sub* a (- b a)))))
12+
(euclid-sub* (abs a) (abs b)))
13+
14+
(defun euclid-mod (a b)
15+
"Finds the greatest common divisor for any two integers"
16+
(if (zerop b)
17+
(abs a)
18+
(euclid-mod b (mod a b))))
19+
20+
(print (euclid-sub (* 64 67) (* 64 81)))
21+
(print (euclid-mod (* 128 12) (* 128 77)))
22+
23+
;; Quick test
24+
(assert
25+
(eql (euclid-sub (* 64 67) (* 64 81))
26+
(gcd (* 64 67) (* 64 81))))
27+
28+
(assert
29+
(eql (euclid-mod (* 64 67) (* 64 81))
30+
(gcd (* 64 67) (* 64 81))))

contents/euclidean_algorithm/code/clisp/euclidean_algorithm.lisp

-33
This file was deleted.

contents/euclidean_algorithm/euclidean_algorithm.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
2020
{% sample lang="js" %}
2121
[import:15-29, lang="javascript"](code/javascript/euclidean_example.js)
2222
{% sample lang="lisp" %}
23-
[import:3-12, lang="lisp"](code/clisp/euclidean_algorithm.lisp)
23+
[import:3-12, lang="lisp"](code/clisp/euclidean.lisp)
2424
{% sample lang="py" %}
2525
[import:11-22, lang="python"](code/python/euclidean_example.py)
2626
{% sample lang="haskell" %}
@@ -102,7 +102,7 @@ Modern implementations, though, often use the modulus operator (%) like so
102102
{% sample lang="js" %}
103103
[import:1-13, lang="javascript"](code/javascript/euclidean_example.js)
104104
{% sample lang="lisp" %}
105-
[import:13-17, lang="lisp"](code/clisp/euclidean_algorithm.lisp)
105+
[import:14-18, lang="lisp"](code/clisp/euclidean.lisp)
106106
{% sample lang="py" %}
107107
[import:1-9, lang="python"](code/python/euclidean_example.py)
108108
{% sample lang="haskell" %}
@@ -195,7 +195,7 @@ Here's a video on the Euclidean algorithm:
195195
{% sample lang="js" %}
196196
[import, lang="javascript"](code/javascript/euclidean_example.js)
197197
{% sample lang="lisp" %}
198-
[import, lang="lisp"](code/clisp/euclidean_algorithm.lisp)
198+
[import, lang="lisp"](code/clisp/euclidean.lisp)
199199
{% sample lang="py" %}
200200
[import, lang="python"](code/python/euclidean_example.py)
201201
{% sample lang="haskell" %}

0 commit comments

Comments
 (0)