Skip to content

Commit 7d15d3b

Browse files
c252leios
authored andcommitted
implemented euclidean algorithm in Scratch (#614)
* implemented euclidean algorithm in scratch 3 * added link to scratch project
1 parent e067fcb commit 7d15d3b

File tree

5 files changed

+384
-0
lines changed

5 files changed

+384
-0
lines changed

contents/euclidean_algorithm/code/scratch/euclid_mod.svg

Lines changed: 114 additions & 0 deletions
Loading

contents/euclidean_algorithm/code/scratch/euclid_sub.svg

Lines changed: 114 additions & 0 deletions
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
define euclid_sub
2+
set [a v] to ([abs v] of [a v])
3+
set [b v] to ([abs v] of [b v])
4+
repeat until <(a) = (b)>
5+
if <(a) > (b)> then
6+
set [a v] to ((a) - (b))
7+
else
8+
set [b v] to ((b) - (a))
9+
10+
11+
define euclid_mod
12+
set [a v] to ([abs v] of [a v])
13+
set [b v] to ([abs v] of [b v])
14+
repeat until <(b) = (0)>
15+
set [temp v] to (b)
16+
set [b v] to ((a) mod (b))
17+
set [a v] to (temp)
18+
19+
when green flag clicked
20+
set [a v] to ((64) * (67))
21+
set [b v] to ((64) * (81))
22+
euclid_sub
23+
set [a v] to ((128) * (12))
24+
set [b v] to ((128) * (77))
25+
euclid_mod

contents/euclidean_algorithm/code/scratch/main.svg

Lines changed: 114 additions & 0 deletions
Loading

contents/euclidean_algorithm/euclidean_algorithm.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
7171
> ![](code/piet/subtract/euclidian_algorithm_subtract_large.png) ![](code/piet/subtract/euclidian_algorithm_subtract.png)
7272
{% sample lang="ss" %}
7373
[import:1-7, lang="scheme"](code/scheme/euclidalg.ss)
74+
{% sample lang="scratch" %}
75+
<p>
76+
<img class="center" src="code/scratch/euclid_sub.svg" width="200" />
77+
</p>
78+
7479
{% endmethod %}
7580

7681
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:
@@ -148,6 +153,11 @@ Modern implementations, though, often use the modulus operator (%) like so
148153
> ![](code/piet/mod/euclidian_algorithm_mod_large.png) ![](code/piet/mod/euclidian_algorithm_mod.png)
149154
{% sample lang="ss" %}
150155
[import:9-12, lang="scheme"](code/scheme/euclidalg.ss)
156+
{% sample lang="scratch" %}
157+
<p>
158+
<img class="center" src="code/scratch/euclid_mod.svg" width="200" />
159+
</p>
160+
151161
{% endmethod %}
152162

153163
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:
@@ -248,6 +258,13 @@ A text version of the program is provided for both versions.
248258
[import:126-146](code/piet/euclidian_algorithm.piet)
249259
{% sample lang="ss" %}
250260
[import:, lang="scheme"](code/scheme/euclidalg.ss)
261+
{% sample lang="scratch" %}
262+
The code snippets were taken from this [Scratch project](https://scratch.mit.edu/projects/278727055/)
263+
264+
<p>
265+
<img class="center" src="code/scratch/main.svg" width="200" />
266+
</p>
267+
251268
{% endmethod %}
252269

253270
<script>

0 commit comments

Comments
 (0)