Skip to content

Add Euclidean GCD in Factor #425

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 7 commits into from
Oct 8, 2018
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
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ indent_size = 2
indent_style = space
indent_size = 4

# Factor
[*.factor]
intent_style = space
indent_size = 2

#Lua
[*.lua]
indent_style = space
Expand Down
4 changes: 4 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@
"lang": "f90",
"name": "Fortran90"
},
{
"lang": "factor",
"name": "Factor"
},
{
"lang": "ws",
"name": "Whitespace"
Expand Down
32 changes: 32 additions & 0 deletions contents/euclidean_algorithm/code/factor/euclid.factor
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
: euclid- ( a b -- gcd )
[ abs ] bi@
[ 2dup = ]
[
! make sure the lower number is deeper
2dup >= [ swap ] when
over -
! leaves us with stack { <lower> <greater - lower> }
]
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
;

42 56 euclid% . ! 14
48 180 euclid% . ! 12

42 56 euclid- . ! 14
48 180 euclid- . ! 12

6 changes: 6 additions & 0 deletions contents/euclidean_algorithm/euclidean_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-13, lang="factor"](code/factor/euclid.factor)
{% sample lang="ws" %}
[import, lang="whitespace"](code/whitespace/euclidian_sub.ws)
{% sample lang="scala" %}
Expand Down Expand Up @@ -98,6 +100,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:15-25, lang="factor"](code/factor/euclid.factor)
{% sample lang="ws" %}
[import, lang="whitespace"](code/whitespace/euclidian_mod.ws)
{% sample lang="scala" %}
Expand Down Expand Up @@ -160,6 +164,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)
{% sample lang="ws" %}
Here is a readable version of the algorithms with comments. First, subtraction method:
[import, lang="whitespace"](code/whitespace/euclidian_sub_comments.ws)
Expand Down