Skip to content

Minor changes to euclidean algorithm implementation in Common Lisp #609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
30 changes: 30 additions & 0 deletions contents/euclidean_algorithm/code/clisp/euclidean.lisp
Original file line number Diff line number Diff line change
@@ -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))))
33 changes: 0 additions & 33 deletions contents/euclidean_algorithm/code/clisp/euclidean_algorithm.lisp

This file was deleted.

6 changes: 3 additions & 3 deletions contents/euclidean_algorithm/euclidean_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down Expand Up @@ -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" %}
Expand Down