Skip to content

Commit fc250ee

Browse files
Nic Hartleyjiegillet
Nic Hartley
authored andcommitted
Add Euclidean GCD in Factor (#425)
* Factor Euclidean GCD * added .editorconfig changes * update file to follow new editorconfig * vim, stop adding tabs, kthxbye * added absolute values
1 parent 1ffa3b8 commit fc250ee

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

.editorconfig

+5
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ indent_size = 2
133133
indent_style = space
134134
indent_size = 4
135135

136+
# Factor
137+
[*.factor]
138+
intent_style = space
139+
indent_size = 2
140+
136141
#Lua
137142
[*.lua]
138143
indent_style = space

book.json

+4
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@
144144
"lang": "f90",
145145
"name": "Fortran90"
146146
},
147+
{
148+
"lang": "factor",
149+
"name": "Factor"
150+
},
147151
{
148152
"lang": "ws",
149153
"name": "Whitespace"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
: euclid- ( a b -- gcd )
2+
[ abs ] bi@
3+
[ 2dup = ]
4+
[
5+
! make sure the lower number is deeper
6+
2dup >= [ swap ] when
7+
over -
8+
! leaves us with stack { <lower> <greater - lower> }
9+
]
10+
until
11+
! we have the GCD twice now, drop one
12+
drop
13+
;
14+
15+
: euclid% ( a b -- gcd )
16+
[ abs ] bi@ ! take both absolute values
17+
[ dup zero? ] ! check if `b` (on top) is 0
18+
[
19+
! a b -> a b b -> b a b -> b a%b
20+
dup -rot mod
21+
]
22+
until
23+
! the zero is on top, so get rid of it
24+
drop
25+
;
26+
27+
42 56 euclid% . ! 14
28+
48 180 euclid% . ! 12
29+
30+
42 56 euclid- . ! 14
31+
48 180 euclid- . ! 12
32+

contents/euclidean_algorithm/euclidean_algorithm.md

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two
4141
[import:13-24, lang="nim"](code/nim/euclid_algorithm.nim)
4242
{% sample lang="f90" %}
4343
[import:1-19, lang="fortran"](code/fortran/euclidean.f90)
44+
{% sample lang="factor" %}
45+
[import:1-13, lang="factor"](code/factor/euclid.factor)
4446
{% sample lang="ws" %}
4547
[import, lang="whitespace"](code/whitespace/euclidian_sub.ws)
4648
{% sample lang="scala" %}
@@ -100,6 +102,8 @@ Modern implementations, though, often use the modulus operator (%) like so
100102
[import:1-11, lang="nim"](code/nim/euclid_algorithm.nim)
101103
{% sample lang="f90" %}
102104
[import:21-34, lang="fortran"](code/fortran/euclidean.f90)
105+
{% sample lang="factor" %}
106+
[import:15-25, lang="factor"](code/factor/euclid.factor)
103107
{% sample lang="ws" %}
104108
[import, lang="whitespace"](code/whitespace/euclidian_mod.ws)
105109
{% sample lang="scala" %}
@@ -164,6 +168,8 @@ The Euclidean Algorithm is truly fundamental to many other algorithms throughout
164168
[import, lang="nim" %](code/nim/euclid_algorithm.nim)
165169
{% sample lang="f90" %}
166170
[import, lang="fortran"](code/fortran/euclidean.f90)
171+
{% sample lang="factor" %}
172+
[import, lang="factor"](code/factor/euclid.factor)
167173
{% sample lang="ws" %}
168174
Here is a readable version of the algorithms with comments. First, subtraction method:
169175
[import, lang="whitespace"](code/whitespace/euclidian_sub_comments.ws)

0 commit comments

Comments
 (0)