Skip to content

implemented euclidean algorithm in Scratch #614

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

Merged
merged 2 commits into from
Jul 10, 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
114 changes: 114 additions & 0 deletions contents/euclidean_algorithm/code/scratch/euclid_mod.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 114 additions & 0 deletions contents/euclidean_algorithm/code/scratch/euclid_sub.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions contents/euclidean_algorithm/code/scratch/euclidean.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
define euclid_sub
set [a v] to ([abs v] of [a v])
set [b v] to ([abs v] of [b v])
repeat until <(a) = (b)>
if <(a) > (b)> then
set [a v] to ((a) - (b))
else
set [b v] to ((b) - (a))


define euclid_mod
set [a v] to ([abs v] of [a v])
set [b v] to ([abs v] of [b v])
repeat until <(b) = (0)>
set [temp v] to (b)
set [b v] to ((a) mod (b))
set [a v] to (temp)

when green flag clicked
set [a v] to ((64) * (67))
set [b v] to ((64) * (81))
euclid_sub
set [a v] to ((128) * (12))
set [b v] to ((128) * (77))
euclid_mod
114 changes: 114 additions & 0 deletions contents/euclidean_algorithm/code/scratch/main.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions contents/euclidean_algorithm/euclidean_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
> ![](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)
{% sample lang="scratch" %}
<p>
<img class="center" src="code/scratch/euclid_sub.svg" width="200" />
</p>

{% 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 @@ -148,6 +153,11 @@ Modern implementations, though, often use the modulus operator (%) like so
> ![](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)
{% sample lang="scratch" %}
<p>
<img class="center" src="code/scratch/euclid_mod.svg" width="200" />
</p>

{% 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 @@ -248,6 +258,13 @@ A text version of the program is provided for both versions.
[import:126-146](code/piet/euclidian_algorithm.piet)
{% sample lang="ss" %}
[import:, lang="scheme"](code/scheme/euclidalg.ss)
{% sample lang="scratch" %}
The code snippets were taken from this [Scratch project](https://scratch.mit.edu/projects/278727055/)

<p>
<img class="center" src="code/scratch/main.svg" width="200" />
</p>

{% endmethod %}

<script>
Expand Down