Skip to content

Commit 3cb3b53

Browse files
PaddyKejiegillet
authored andcommitted
Euclidean algorithm racket (#433)
* Add Euclidean algorithm in Racket * Added username to CONTRIBUTORS.md * Added euclid_mod* wrapper
1 parent cade450 commit 3cb3b53

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

CONTRIBUTORS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ This file lists everyone, who contributed to this repo and wanted to show up her
3434
- Trashtalk
3535
- Cyrus Burt
3636
- Patrik Tesarik
37-
- Ken Power
37+
- Ken Power
38+
- PaddyKe
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#lang racket
2+
3+
(define (euclid_sub a b)
4+
(local ((define (euclid_sub* x y)
5+
(if (= x y)
6+
x
7+
(if (> x y)
8+
(euclid_sub* (- x y) y)
9+
(euclid_sub* x (- y x))
10+
)
11+
)
12+
)) (euclid_sub* (abs a) (abs b))
13+
)
14+
)
15+
16+
(define (euclid_mod a b)
17+
(local ((define (euclid_mod* a b)
18+
(if (= 0 b)
19+
(abs a)
20+
(euclid_mod* b (modulo a b))
21+
)
22+
)) (euclid_mod* a b)
23+
)
24+
)
25+
26+
(displayln (euclid_sub (* 64 67) (* 64 81)))
27+
(displayln (euclid_mod (* 128 12) (* 128 77)))

contents/euclidean_algorithm/euclidean_algorithm.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
4141
[import:13-24, lang="nim"](code/nim/euclid_algorithm.nim)
4242
{% sample lang="f90" %}
4343
[import:1-19, lang="fortran"](code/fortran/euclidean.f90)
44+
{% sample lang="racket" %}
45+
[import:3-14, lang="lisp"](code/racket/euclidean_algorithm.rkt)
4446
{% endmethod %}
4547

4648
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
8890
[import:1-11, lang="nim"](code/nim/euclid_algorithm.nim)
8991
{% sample lang="f90" %}
9092
[import:21-34, lang="fortran"](code/fortran/euclidean.f90)
93+
{% sample lang="racket" %}
94+
[import:16-24, lang="lisp"](code/racket/euclidean_algorithm.rkt)
9195
{% endmethod %}
9296

9397
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
140144
[import, lang="nim" %](code/nim/euclid_algorithm.nim)
141145
{% sample lang="f90" %}
142146
[import, lang="fortran"](code/fortran/euclidean.f90)
147+
{% sample lang="racket" %}
148+
[import, lang="lisp"](code/racket/euclidean_algorithm.rkt)
143149
{% endmethod %}
144150

145151

0 commit comments

Comments
 (0)