diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index d88ff80a3..4cf9d5069 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -34,4 +34,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her - Trashtalk - Cyrus Burt - Patrik Tesarik -- Ken Power \ No newline at end of file +- Ken Power +- PaddyKe \ No newline at end of file diff --git a/contents/euclidean_algorithm/code/racket/euclidean_algorithm.rkt b/contents/euclidean_algorithm/code/racket/euclidean_algorithm.rkt new file mode 100755 index 000000000..f170d8e17 --- /dev/null +++ b/contents/euclidean_algorithm/code/racket/euclidean_algorithm.rkt @@ -0,0 +1,27 @@ +#lang racket + +(define (euclid_sub a b) + (local ((define (euclid_sub* x y) + (if (= x y) + x + (if (> x y) + (euclid_sub* (- x y) y) + (euclid_sub* x (- y x)) + ) + ) + )) (euclid_sub* (abs a) (abs b)) + ) + ) + +(define (euclid_mod a b) + (local ((define (euclid_mod* a b) + (if (= 0 b) + (abs a) + (euclid_mod* b (modulo a b)) + ) + )) (euclid_mod* a b) + ) + ) + +(displayln (euclid_sub (* 64 67) (* 64 81))) +(displayln (euclid_mod (* 128 12) (* 128 77))) diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 8620fa1ee..712577edb 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -41,6 +41,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two [import:13-24, lang="nim"](code/nim/euclid_algorithm.nim) {% sample lang="f90" %} [import:1-19, lang="fortran"](code/fortran/euclidean.f90) +{% sample lang="racket" %} +[import:3-14, lang="lisp"](code/racket/euclidean_algorithm.rkt) {% endmethod %} Here, we simply line the two numbers up every step and subtract the lower value from the higher one every timestep. Once the two values are equal, we call that value the greatest common divisor. A graph of `a` and `b` as they change every step would look something like this: @@ -88,6 +90,8 @@ Modern implementations, though, often use the modulus operator (%) like so [import:1-11, lang="nim"](code/nim/euclid_algorithm.nim) {% sample lang="f90" %} [import:21-34, lang="fortran"](code/fortran/euclidean.f90) +{% sample lang="racket" %} +[import:16-24, lang="lisp"](code/racket/euclidean_algorithm.rkt) {% endmethod %} Here, we set `b` to be the remainder of `a%b` and `a` to be whatever `b` was last timestep. Because of how the modulus operator works, this will provide the same information as the subtraction-based implementation, but when we show `a` and `b` as they change with time, we can see that it might take many fewer steps: @@ -140,6 +144,8 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout [import, lang="nim" %](code/nim/euclid_algorithm.nim) {% sample lang="f90" %} [import, lang="fortran"](code/fortran/euclidean.f90) +{% sample lang="racket" %} +[import, lang="lisp"](code/racket/euclidean_algorithm.rkt) {% endmethod %}