From 2cc71f7e4ca590154c38e263d6a0a25ba78b9d73 Mon Sep 17 00:00:00 2001 From: Neverik Date: Fri, 5 Oct 2018 10:37:04 +0700 Subject: [PATCH 1/2] Euclidean algorithm for Smalltalk. --- .../code/smalltalk/euclid.st | 29 +++++++++++++++++++ .../euclidean_algorithm.md | 6 ++++ 2 files changed, 35 insertions(+) create mode 100644 contents/euclidean_algorithm/code/smalltalk/euclid.st diff --git a/contents/euclidean_algorithm/code/smalltalk/euclid.st b/contents/euclidean_algorithm/code/smalltalk/euclid.st new file mode 100644 index 000000000..f23522d3a --- /dev/null +++ b/contents/euclidean_algorithm/code/smalltalk/euclid.st @@ -0,0 +1,29 @@ +Integer>>euclidSub: secondNumber + "Euclidean algorithm with subtraction" + | a b | + a := self abs. + b := secondNumber abs. + [ a == b ] whileFalse: [ + a > b ifTrue: [ + a := a - b. + ] ifFalse: [ + b := b - a. + ]. + ]. + ^a. + +Integer>>euclidDiv: secondNumber + "Euclidean algorithm with division" + | a b oldB | + a := self abs. + b := secondNumber abs. + [ b == 0 ] whileFalse: [ + oldB := b. + b := a % b. + a := oldB. + ]. + ^a. + +Transcript show: ((64 * 67) euclidSub: (64 * 81)). +Transcript cr. +Transcript show: ((128 * 12) euclidDiv: (128 * 77)). diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 712577edb..d2c0d9131 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -43,6 +43,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two [import:1-19, lang="fortran"](code/fortran/euclidean.f90) {% sample lang="racket" %} [import:3-14, lang="lisp"](code/racket/euclidean_algorithm.rkt) +{% sample lang="st" %} +[import:1-13, lang="smalltalk"](code/smalltalk/euclid.st) {% 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: @@ -92,6 +94,8 @@ Modern implementations, though, often use the modulus operator (%) like so [import:21-34, lang="fortran"](code/fortran/euclidean.f90) {% sample lang="racket" %} [import:16-24, lang="lisp"](code/racket/euclidean_algorithm.rkt) +{% sample lang="st" %} +[import:15-25, lang="smalltalk"](code/smalltalk/euclid.st) {% 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: @@ -146,6 +150,8 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout [import, lang="fortran"](code/fortran/euclidean.f90) {% sample lang="racket" %} [import, lang="lisp"](code/racket/euclidean_algorithm.rkt) +{% sample lang="st" %} +[import, lang="smalltalk"](code/smalltalk/euclid.st) {% endmethod %} From 86a8fe97bfa43d1d5af0a7d2d17886c4c4e6a080 Mon Sep 17 00:00:00 2001 From: Stepan Date: Fri, 5 Oct 2018 10:58:16 +0700 Subject: [PATCH 2/2] Fixed indent --- .../code/smalltalk/euclid.st | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/contents/euclidean_algorithm/code/smalltalk/euclid.st b/contents/euclidean_algorithm/code/smalltalk/euclid.st index f23522d3a..95d6463f2 100644 --- a/contents/euclidean_algorithm/code/smalltalk/euclid.st +++ b/contents/euclidean_algorithm/code/smalltalk/euclid.st @@ -1,29 +1,29 @@ Integer>>euclidSub: secondNumber - "Euclidean algorithm with subtraction" - | a b | - a := self abs. + "Euclidean algorithm with subtraction" + | a b | + a := self abs. b := secondNumber abs. - [ a == b ] whileFalse: [ - a > b ifTrue: [ - a := a - b. - ] ifFalse: [ - b := b - a. - ]. - ]. - ^a. + [ a == b ] whileFalse: [ + a > b ifTrue: [ + a := a - b. + ] ifFalse: [ + b := b - a. + ]. + ]. + ^a. -Integer>>euclidDiv: secondNumber - "Euclidean algorithm with division" - | a b oldB | - a := self abs. +Integer>>euclidMod: secondNumber + "Euclidean algorithm with modulus." + | a b oldB | + a := self abs. b := secondNumber abs. - [ b == 0 ] whileFalse: [ - oldB := b. - b := a % b. - a := oldB. - ]. - ^a. + [ b == 0 ] whileFalse: [ + oldB := b. + b := a % b. + a := oldB. + ]. + ^a. Transcript show: ((64 * 67) euclidSub: (64 * 81)). Transcript cr. -Transcript show: ((128 * 12) euclidDiv: (128 * 77)). +Transcript show: ((128 * 12) euclidMod: (128 * 77)).