diff --git a/contents/euclidean_algorithm/code/clisp/euclidean.lisp b/contents/euclidean_algorithm/code/clisp/euclidean.lisp new file mode 100644 index 000000000..62f525ac3 --- /dev/null +++ b/contents/euclidean_algorithm/code/clisp/euclidean.lisp @@ -0,0 +1,30 @@ +;;;; Euclidean algorithm implementation in Common Lisp + +(defun euclid-sub (a b) + "Finds the greatest common divsor for any two integers" + (defun euclid-sub* (a b) + "Finds the greatest common divisor for any two positive integers" + (if (eql a b) + a + (if (> a b) + (euclid-sub* (- a b) b) + (euclid-sub* a (- b a))))) + (euclid-sub* (abs a) (abs b))) + +(defun euclid-mod (a b) + "Finds the greatest common divisor for any two integers" + (if (zerop b) + (abs a) + (euclid-mod b (mod a b)))) + +(print (euclid-sub (* 64 67) (* 64 81))) +(print (euclid-mod (* 128 12) (* 128 77))) + +;; Quick test +(assert + (eql (euclid-sub (* 64 67) (* 64 81)) + (gcd (* 64 67) (* 64 81)))) + +(assert + (eql (euclid-mod (* 64 67) (* 64 81)) + (gcd (* 64 67) (* 64 81)))) diff --git a/contents/euclidean_algorithm/code/clisp/euclidean_algorithm.lisp b/contents/euclidean_algorithm/code/clisp/euclidean_algorithm.lisp deleted file mode 100644 index cff5ab29f..000000000 --- a/contents/euclidean_algorithm/code/clisp/euclidean_algorithm.lisp +++ /dev/null @@ -1,33 +0,0 @@ -;;;; Euclid algorithm implementation - -(defun euclid-sub (a b) - (euclid-sub* (abs a) (abs b))) - -(defun euclid-sub* (a b) - (if (eq a b) - a - (if (> a b) - (euclid-sub* (- a b) b) - (euclid-sub* a (- b a))))) - -(defun euclid-mod (a b) - (if (zerop b) - (abs a) - (euclid-mod b (mod a b)))) - -(print - (euclid-sub (* 64 67) - (* 64 81))) -(print - (euclid-mod (* 128 12) - (* 128 77))) - -;; built-in function: (gcd 80 40) -;; quick test -(assert - (= (euclid-sub (* 64 67) (* 64 81)) - (gcd (* 64 67) (* 64 81)))) - -(assert - (= (euclid-mod (* 64 67) (* 64 81)) - (gcd (* 64 67) (* 64 81)))) diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 54ab3c3a6..d1ac7e2de 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -20,7 +20,7 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two {% sample lang="js" %} [import:15-29, lang="javascript"](code/javascript/euclidean_example.js) {% sample lang="lisp" %} -[import:3-12, lang="lisp"](code/clisp/euclidean_algorithm.lisp) +[import:3-12, lang="lisp"](code/clisp/euclidean.lisp) {% sample lang="py" %} [import:11-22, lang="python"](code/python/euclidean_example.py) {% sample lang="haskell" %} @@ -102,7 +102,7 @@ Modern implementations, though, often use the modulus operator (%) like so {% sample lang="js" %} [import:1-13, lang="javascript"](code/javascript/euclidean_example.js) {% sample lang="lisp" %} -[import:13-17, lang="lisp"](code/clisp/euclidean_algorithm.lisp) +[import:14-18, lang="lisp"](code/clisp/euclidean.lisp) {% sample lang="py" %} [import:1-9, lang="python"](code/python/euclidean_example.py) {% sample lang="haskell" %} @@ -195,7 +195,7 @@ Here's a video on the Euclidean algorithm: {% sample lang="js" %} [import, lang="javascript"](code/javascript/euclidean_example.js) {% sample lang="lisp" %} -[import, lang="lisp"](code/clisp/euclidean_algorithm.lisp) +[import, lang="lisp"](code/clisp/euclidean.lisp) {% sample lang="py" %} [import, lang="python"](code/python/euclidean_example.py) {% sample lang="haskell" %}