From 73c2c48849d24b22b1d68ce0ed9b582b200f1985 Mon Sep 17 00:00:00 2001 From: Nic Hartley Date: Tue, 2 Oct 2018 20:40:30 -0700 Subject: [PATCH 1/5] Factor Euclidean GCD --- book.json | 6 ++++- .../code/factor/euclid.factor | 27 +++++++++++++++++++ .../euclidean_algorithm.md | 6 +++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 contents/euclidean_algorithm/code/factor/euclid.factor diff --git a/book.json b/book.json index 94676c45b..705d244f0 100644 --- a/book.json +++ b/book.json @@ -139,7 +139,11 @@ { "lang": "f90", "name": "Fortran90" - } + }, + { + "lang": "factor", + "name": "Factor" + } ] } } diff --git a/contents/euclidean_algorithm/code/factor/euclid.factor b/contents/euclidean_algorithm/code/factor/euclid.factor new file mode 100644 index 000000000..3352a3331 --- /dev/null +++ b/contents/euclidean_algorithm/code/factor/euclid.factor @@ -0,0 +1,27 @@ +: euclid- ( a b -- gcd ) + [ 2dup = ] + [ + ! make sure the lower number is deeper + 2dup >= [ swap ] when ! a b -> G(reater) L(ess) + over - ! leaves us with L G-L + ] + until + ! we have the GCD twice now, drop one + drop ; + +: euclid% ( a b -- gcd ) + [ dup zero? ] + [ + ! a b -> a b b -> b a b -> b a%b + dup -rot mod + ] + until + ! the zero is on top, so get rid of it + drop ; + +42 56 euclid% . ! 14 +48 180 euclid% . ! 12 + +42 56 euclid- . ! 14 +48 180 euclid- . ! 12 + diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 8620fa1ee..a71862610 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -41,6 +41,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two [import:13-24, lang="nim"](code/nim/euclid_algorithm.nim) {% sample lang="f90" %} [import:1-19, lang="fortran"](code/fortran/euclidean.f90) +{% sample lang="factor" %} +[import:1-10, lang="factor"](code/factor/euclid.factor) {% 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: @@ -88,6 +90,8 @@ Modern implementations, though, often use the modulus operator (%) like so [import:1-11, lang="nim"](code/nim/euclid_algorithm.nim) {% sample lang="f90" %} [import:21-34, lang="fortran"](code/fortran/euclidean.f90) +{% sample lang="factor" %} +[import:12-20, lang="factor"](code/factor/euclid.factor) {% 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: @@ -140,6 +144,8 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout [import, lang="nim" %](code/nim/euclid_algorithm.nim) {% sample lang="f90" %} [import, lang="fortran"](code/fortran/euclidean.f90) +{% sample lang="factor" %} +[import, lang="factor"](code/factor/euclid.factor) {% endmethod %} From 72e3e29df9665bf4f9fd4a60cb5f1fef6524e47e Mon Sep 17 00:00:00 2001 From: Nic Hartley Date: Sat, 6 Oct 2018 09:43:59 -0700 Subject: [PATCH 2/5] added .editorconfig changes --- .editorconfig | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.editorconfig b/.editorconfig index 843ef4f6c..a59c22b7b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -119,3 +119,9 @@ indent_size = 2 [*.php] indent_style = space indent_size = 4 + +# Factor +[*.factor] +intent_style = space +indent_size = 2 + From 057046b3704b8b5ef5aafb42718f08256bb483c1 Mon Sep 17 00:00:00 2001 From: Nic Hartley Date: Sat, 6 Oct 2018 09:46:28 -0700 Subject: [PATCH 3/5] update file to follow new editorconfig --- .../code/factor/euclid.factor | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/contents/euclidean_algorithm/code/factor/euclid.factor b/contents/euclidean_algorithm/code/factor/euclid.factor index f8b6f13bb..be4fa89e8 100644 --- a/contents/euclidean_algorithm/code/factor/euclid.factor +++ b/contents/euclidean_algorithm/code/factor/euclid.factor @@ -1,25 +1,27 @@ : euclid- ( a b -- gcd ) - [ 2dup = ] - [ - ! make sure the lower number is deeper - 2dup >= [ swap ] when - over - - ! leaves us with stack { } - ] - until - ! we have the GCD twice now, drop one - drop ; + [ 2dup = ] + [ + ! make sure the lower number is deeper + 2dup >= [ swap ] when + over - + ! leaves us with stack { } + ] + until + ! we have the GCD twice now, drop one + drop +; : euclid% ( a b -- gcd ) - [ abs ] bi@ ! take both absolute values - [ dup zero? ] ! check if `b` (on top) is 0 - [ - ! a b -> a b b -> b a b -> b a%b - dup -rot mod - ] - until - ! the zero is on top, so get rid of it - drop ; + [ abs ] bi@ ! take both absolute values + [ dup zero? ] ! check if `b` (on top) is 0 + [ + ! a b -> a b b -> b a b -> b a%b + dup -rot mod + ] + until + ! the zero is on top, so get rid of it + drop +; 42 56 euclid% . ! 14 48 180 euclid% . ! 12 From 13601b98eef3dfe8c02150c08d6eae7117ef6dc9 Mon Sep 17 00:00:00 2001 From: Nic Hartley Date: Sun, 7 Oct 2018 10:21:52 -0700 Subject: [PATCH 4/5] vim, stop adding tabs, kthxbye --- book.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book.json b/book.json index 2f307f24f..8bc8dfee9 100644 --- a/book.json +++ b/book.json @@ -147,7 +147,7 @@ { "lang": "factor", "name": "Factor" - }, + }, { "lang": "scala", "name": "Scala" From 48199406b31586091b500181a0326e566d17426b Mon Sep 17 00:00:00 2001 From: Nic Hartley Date: Sun, 7 Oct 2018 10:26:54 -0700 Subject: [PATCH 5/5] added absolute values --- contents/euclidean_algorithm/code/factor/euclid.factor | 1 + contents/euclidean_algorithm/euclidean_algorithm.md | 7 ++----- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/contents/euclidean_algorithm/code/factor/euclid.factor b/contents/euclidean_algorithm/code/factor/euclid.factor index be4fa89e8..e822d1105 100644 --- a/contents/euclidean_algorithm/code/factor/euclid.factor +++ b/contents/euclidean_algorithm/code/factor/euclid.factor @@ -1,4 +1,5 @@ : euclid- ( a b -- gcd ) + [ abs ] bi@ [ 2dup = ] [ ! make sure the lower number is deeper diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 148f7d027..0afd66038 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -41,13 +41,10 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two [import:13-24, lang="nim"](code/nim/euclid_algorithm.nim) {% sample lang="f90" %} [import:1-19, lang="fortran"](code/fortran/euclidean.f90) -<<<<<<< HEAD {% sample lang="factor" %} -[import:1-11, lang="factor"](code/factor/euclid.factor) -======= +[import:1-13, lang="factor"](code/factor/euclid.factor) {% sample lang="ws" %} [import, lang="whitespace"](code/whitespace/euclidian_sub.ws) ->>>>>>> c6ff7bbfcae6cb054ac21573d8b36f8cbf018471 {% sample lang="scala" %} [import:3-8, lang="scala"](code/scala/euclidean.scala) {% sample lang="racket" %} @@ -104,7 +101,7 @@ Modern implementations, though, often use the modulus operator (%) like so {% sample lang="f90" %} [import:21-34, lang="fortran"](code/fortran/euclidean.f90) {% sample lang="factor" %} -[import:13-22, lang="factor"](code/factor/euclid.factor) +[import:15-25, lang="factor"](code/factor/euclid.factor) {% sample lang="ws" %} [import, lang="whitespace"](code/whitespace/euclidian_mod.ws) {% sample lang="scala" %}