Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

euclid algorithm in scheme #582

Merged
merged 11 commits into from
Feb 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@
{
"lang": "piet",
"name": "Piet"
},
{
"lang": "ss",
"name": "Scheme"
}
]
}
Expand Down
15 changes: 15 additions & 0 deletions contents/euclidean_algorithm/code/scheme/euclidalg.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
(define (euclid-sub a b)
(cond
[(or (negative? a)(negative? b))(euclid-sub (abs a)(abs b))]
[(eq? a b) a]
[(> a b)(euclid-sub(- a b) b)]
[else
(euclid-sub a (- b a))]))

(define (euclid-mod a b)
(if (zero? b)
a
(euclid-mod b (modulo a b))))

(display (euclid-mod (* 64 67) (* 64 81))) (newline)
(display (euclid-sub (* 64 12) (* 64 27))) (newline)
6 changes: 6 additions & 0 deletions contents/euclidean_algorithm/euclidean_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
[import:24-38, lang="bash"](code/bash/euclid.bash)
{% sample lang="piet" %}
> ![](code/piet/subtract/euclidian_algorithm_subtract_large.png) ![](code/piet/subtract/euclidian_algorithm_subtract.png)
{% sample lang="ss" %}
[import:1-7, lang="scheme"](code/scheme/euclidalg.ss)
{% 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:
Expand Down Expand Up @@ -136,6 +138,8 @@ Modern implementations, though, often use the modulus operator (%) like so
[import:10-22, lang="bash"](code/bash/euclid.bash)
{% sample lang="piet" %}
> ![](code/piet/mod/euclidian_algorithm_mod_large.png) ![](code/piet/mod/euclidian_algorithm_mod.png)
{% sample lang="ss" %}
[import:9-12, lang="scheme"](code/scheme/euclidalg.ss)
{% 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:
Expand Down Expand Up @@ -232,6 +236,8 @@ A text version of the program is provided for both versions.
> ![](code/piet/mod/euclidian_algorithm_mod_large.png) ![](code/piet/mod/euclidian_algorithm_mod.png)

[import:126-146](code/piet/euclidian_algorithm.piet)
{% sample lang="ss" %}
[import:, lang="scheme"](code/scheme/euclidalg.ss)
{% endmethod %}

<script>
Expand Down