From 7b5c628e89c834a3e8f018be0b1f3f146965bf0f Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Fri, 31 May 2019 16:02:13 +0200 Subject: [PATCH 1/5] 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/5] 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 9f0a2be5049e05bdc91c1f983fdb46bc356d99f4 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Tue, 4 Jun 2019 17:04:57 +0200 Subject: [PATCH 3/5] First version of forward euler in Common Lisp. --- .../code/clisp/euler.lisp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 contents/forward_euler_method/code/clisp/euler.lisp diff --git a/contents/forward_euler_method/code/clisp/euler.lisp b/contents/forward_euler_method/code/clisp/euler.lisp new file mode 100644 index 000000000..8f697de3e --- /dev/null +++ b/contents/forward_euler_method/code/clisp/euler.lisp @@ -0,0 +1,29 @@ +;;;; Forward euler implementation in Common Lisp + +(defun solve-euler (timestep n) + "Returns a function where y'(t) = -3t and y(0) = 0 using the forward euler method" + (loop + with result = (make-array n :initial-element 1) + for i from 1 upto (1- n) do + (setf (svref result i) (- (svref result (1- i)) (* 3 (svref result (1- i)) timestep))) + finally (return result))) + +(defun approximatep (result threshold timestep) + "Checks the result from the solve-euler function" + (loop + with approximatep = t + with solution = 0 + for i from 0 upto (1- (length result)) do + (setf solution (exp (* (- 3) i timestep))) + (when (> (- (svref result i) solution) threshold) + (setf approximatep nil) + (format t "~d ~d~%" (svref result i) solution)) + finally (return approximatep))) + +(defvar timestep 0.01) +(defvar n 100) ;number of steps +(defvar threshold 0.01) + +(defvar result (solve-euler timestep n)) +(defvar approximatep (approximatep result threshold timestep)) +(format t "~:[Value(s) not in threshold~;All values within threshold~]~%" approximatep) From d1fcdca11f5f110ae3b5e6bf385432e85c1aa408 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Tue, 4 Jun 2019 17:08:04 +0200 Subject: [PATCH 4/5] Added common lisp code to .md file. --- contents/forward_euler_method/forward_euler_method.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contents/forward_euler_method/forward_euler_method.md b/contents/forward_euler_method/forward_euler_method.md index 39a2f5821..d951ac14b 100644 --- a/contents/forward_euler_method/forward_euler_method.md +++ b/contents/forward_euler_method/forward_euler_method.md @@ -142,6 +142,8 @@ Full code for the visualization follows: [import, lang:"java"](code/java/ForwardEuler.java) {% sample lang="nim" %} [import, lang:"nim"](code/nim/forwardeuler.nim) +{% sample lang="lisp" %} +[import, lang="lisp"](code/clisp/euler.lisp) {% endmethod %}