From 7b5c628e89c834a3e8f018be0b1f3f146965bf0f Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Fri, 31 May 2019 16:02:13 +0200 Subject: [PATCH 1/7] Added a proof for the Euclidean Algorithm --- contents/euclidean_algorithm/euclidean_algorithm.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 4ff49152f..26c308501 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -166,6 +166,12 @@ Here's a video on the Euclidean algorithm: +## Proof + +Some intuition as to why the Euclidean Algorithm works lies in it's proof. Only a proof for the subtraction method will be given at this point, but the modular version follows the same line of reasoning. + +Given two positive integers $$a$$ and $$b$$, they have a greatest common divisor $$d$$. There is always a common divisor, because every number is divisable by 1. Since $$a$$ and $$b$$ is divisable by $$d$$, $$a - b$$ is also divisable by $$d$$ ($$b < a$$). Let's call this value $$c$$. Now we once more have two numbers $$b$$ and $$c$$, which are both divisable by $$d$$. This process can be continued until the values are equal: this is the greatest common divisor $$d$$. + ## Example Code {% method %} From 131ae549231b39174f6379b0247af56efc827b9c Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Fri, 31 May 2019 16:06:17 +0200 Subject: [PATCH 2/7] Revert "Added a proof for the Euclidean Algorithm" This reverts commit 7b5c628e89c834a3e8f018be0b1f3f146965bf0f. --- contents/euclidean_algorithm/euclidean_algorithm.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 26c308501..4ff49152f 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -166,12 +166,6 @@ Here's a video on the Euclidean algorithm: -## Proof - -Some intuition as to why the Euclidean Algorithm works lies in it's proof. Only a proof for the subtraction method will be given at this point, but the modular version follows the same line of reasoning. - -Given two positive integers $$a$$ and $$b$$, they have a greatest common divisor $$d$$. There is always a common divisor, because every number is divisable by 1. Since $$a$$ and $$b$$ is divisable by $$d$$, $$a - b$$ is also divisable by $$d$$ ($$b < a$$). Let's call this value $$c$$. Now we once more have two numbers $$b$$ and $$c$$, which are both divisable by $$d$$. This process can be continued until the values are equal: this is the greatest common divisor $$d$$. - ## Example Code {% method %} From 0b2b247b4da573dcef7914dce3068034288fdb68 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Sat, 1 Jun 2019 23:26:44 +0200 Subject: [PATCH 3/7] First working version thomas algorithm in lisp. --- .../thomas_algorithm/code/clisp/thomas.lisp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 contents/thomas_algorithm/code/clisp/thomas.lisp diff --git a/contents/thomas_algorithm/code/clisp/thomas.lisp b/contents/thomas_algorithm/code/clisp/thomas.lisp new file mode 100644 index 000000000..c0da247bd --- /dev/null +++ b/contents/thomas_algorithm/code/clisp/thomas.lisp @@ -0,0 +1,30 @@ +;;;; Thomas algorithm implementation in Common Lisp + +(defun thomas (a b c d) + "Returns the solutions to a tri-diagonal matrix destructively" + (setf (svref c 0) (/ (svref c 0) (svref b 0))) + (setf (svref d 0) (/ (svref d 0) (svref b 0))) + + (loop + for i from 1 upto (1- (length a)) do + (setf + (svref c i) + (/ (svref c i) (- (svref b i) (* (svref a i) (svref c (1- i)))))) + (setf + (svref d i) + (/ + (- (svref d i) (* (svref a i) (svref d (1- i)))) + (- (svref b i) (* (svref a i) (svref c (1- i))))))) + + (loop + for i from (- (length a) 2) downto 0 do + (setf (svref d i) (- (svref d i) (* (svref c i) (svref d (1+ i)))))) + + (return-from thomas d)) + +(defvar diagonal-a #(0 2 3)) +(defvar diagonal-b #(1 3 6)) +(defvar diagonal-c #(4 5 0)) +(defvar last-column #(7 5 3)) + +(print (thomas diagonal-a diagonal-b diagonal-c last-column)) From d95332372c4682ab86f706274a67dfb59d667249 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Sat, 1 Jun 2019 23:30:33 +0200 Subject: [PATCH 4/7] Added lisp code to the .md file --- contents/thomas_algorithm/thomas_algorithm.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contents/thomas_algorithm/thomas_algorithm.md b/contents/thomas_algorithm/thomas_algorithm.md index 26ed96b91..a9b1d5f5e 100644 --- a/contents/thomas_algorithm/thomas_algorithm.md +++ b/contents/thomas_algorithm/thomas_algorithm.md @@ -127,6 +127,8 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e [import, lang:"crystal"](code/crystal/thomas.cr) {% sample lang="kotlin" %} [import, lang:"kotlin"](code/kotlin/thomas.kt) +{% sample lang="lisp" %} +[import, lang:"lisp"](code/clisp/thomas.lisp) {% endmethod %}