From 95904e43cd79e4677cf8d12364a9c81f7a7198e6 Mon Sep 17 00:00:00 2001 From: c252 Date: Sat, 15 Jun 2019 12:54:24 -0400 Subject: [PATCH 1/2] implemented euclidean algorithm in scratch 3 --- .../code/scratch/euclid_mod.svg | 114 ++++++++++++++++++ .../code/scratch/euclid_sub.svg | 114 ++++++++++++++++++ .../code/scratch/euclidean.txt | 25 ++++ .../euclidean_algorithm/code/scratch/main.svg | 114 ++++++++++++++++++ .../euclidean_algorithm.md | 15 +++ 5 files changed, 382 insertions(+) create mode 100644 contents/euclidean_algorithm/code/scratch/euclid_mod.svg create mode 100644 contents/euclidean_algorithm/code/scratch/euclid_sub.svg create mode 100644 contents/euclidean_algorithm/code/scratch/euclidean.txt create mode 100644 contents/euclidean_algorithm/code/scratch/main.svg diff --git a/contents/euclidean_algorithm/code/scratch/euclid_mod.svg b/contents/euclidean_algorithm/code/scratch/euclid_mod.svg new file mode 100644 index 000000000..e37bab1d2 --- /dev/null +++ b/contents/euclidean_algorithm/code/scratch/euclid_mod.svg @@ -0,0 +1,114 @@ +defineeuclid_modsetatoabsofasetbtoabsofbrepeatuntilb=0settemptobsetbtoamodbsetatotemp \ No newline at end of file diff --git a/contents/euclidean_algorithm/code/scratch/euclid_sub.svg b/contents/euclidean_algorithm/code/scratch/euclid_sub.svg new file mode 100644 index 000000000..9daa92ec0 --- /dev/null +++ b/contents/euclidean_algorithm/code/scratch/euclid_sub.svg @@ -0,0 +1,114 @@ +defineeuclid_subsetatoabsofasetbtoabsofbrepeatuntila=bifa>bthensetatoa-belsesetbtob-a \ No newline at end of file diff --git a/contents/euclidean_algorithm/code/scratch/euclidean.txt b/contents/euclidean_algorithm/code/scratch/euclidean.txt new file mode 100644 index 000000000..705f576a9 --- /dev/null +++ b/contents/euclidean_algorithm/code/scratch/euclidean.txt @@ -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 diff --git a/contents/euclidean_algorithm/code/scratch/main.svg b/contents/euclidean_algorithm/code/scratch/main.svg new file mode 100644 index 000000000..211fa109b --- /dev/null +++ b/contents/euclidean_algorithm/code/scratch/main.svg @@ -0,0 +1,114 @@ +defineeuclid_subsetatoabsofasetbtoabsofbrepeatuntila=bifa>bthensetatoa-belsesetbtob-adefineeuclid_modsetatoabsofasetbtoabsofbrepeatuntilb=0settemptobsetbtoamodbsetatotempwhenclickedsetato64*67setbto64*81euclid_subsetato128*12setbto128*77euclid_mod \ No newline at end of file diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 4ff49152f..6cdc4ef56 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -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" %} +

+ +

+ {% 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: @@ -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" %} +

+ +

+ {% 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: @@ -248,6 +258,11 @@ 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" %} +

+ +

+ {% endmethod %}