From de0ab21171ca64af9dd3d0887234b2b177d8fba1 Mon Sep 17 00:00:00 2001
From: PaddyKe <34421580+PaddyKe@users.noreply.github.com>
Date: Wed, 3 Oct 2018 19:36:48 +0200
Subject: [PATCH 1/3] Add Euclidean algorithm in Racket
---
.../code/racket/euclidean_algorithm.rkt | 25 +++++++++++++++++++
.../euclidean_algorithm.md | 6 +++++
2 files changed, 31 insertions(+)
create mode 100755 contents/euclidean_algorithm/code/racket/euclidean_algorithm.rkt
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..2ec18748e
--- /dev/null
+++ b/contents/euclidean_algorithm/code/racket/euclidean_algorithm.rkt
@@ -0,0 +1,25 @@
+#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)
+ (if (= 0 b)
+ (abs a)
+ (euclid_mod b (modulo 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..6a39e22cb 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-21, 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 %}
From d48aa90ee279af6449adb44d47cbf49786e9496b Mon Sep 17 00:00:00 2001
From: PaddyKe <34421580+PaddyKe@users.noreply.github.com>
Date: Wed, 3 Oct 2018 20:22:12 +0200
Subject: [PATCH 2/3] Added username to CONTRIBUTORS.md
---
CONTRIBUTORS.md | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md
index 1cbaed966..284963c26 100644
--- a/CONTRIBUTORS.md
+++ b/CONTRIBUTORS.md
@@ -65,4 +65,6 @@ Trashtalk
Cyrus Burt
Patrik Tesarik
-
\ No newline at end of file
+
+PaddyKe
+
From f1e437cce50d2f7ac61964274825cd9725a9fffc Mon Sep 17 00:00:00 2001
From: PaddyKe <34421580+PaddyKe@users.noreply.github.com>
Date: Thu, 4 Oct 2018 09:28:18 +0200
Subject: [PATCH 3/3] Added euclid_mod* wrapper
---
.../code/racket/euclidean_algorithm.rkt | 12 +++++++-----
contents/euclidean_algorithm/euclidean_algorithm.md | 2 +-
2 files changed, 8 insertions(+), 6 deletions(-)
diff --git a/contents/euclidean_algorithm/code/racket/euclidean_algorithm.rkt b/contents/euclidean_algorithm/code/racket/euclidean_algorithm.rkt
index 2ec18748e..f170d8e17 100755
--- a/contents/euclidean_algorithm/code/racket/euclidean_algorithm.rkt
+++ b/contents/euclidean_algorithm/code/racket/euclidean_algorithm.rkt
@@ -14,12 +14,14 @@
)
(define (euclid_mod a b)
- (if (= 0 b)
- (abs a)
- (euclid_mod b (modulo 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 6a39e22cb..712577edb 100644
--- a/contents/euclidean_algorithm/euclidean_algorithm.md
+++ b/contents/euclidean_algorithm/euclidean_algorithm.md
@@ -91,7 +91,7 @@ Modern implementations, though, often use the modulus operator (%) like so
{% sample lang="f90" %}
[import:21-34, lang="fortran"](code/fortran/euclidean.f90)
{% sample lang="racket" %}
-[import:16-21, lang="lisp"](code/racket/euclidean_algorithm.rkt)
+[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: