summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gcd.fs37
1 files changed, 37 insertions, 0 deletions
diff --git a/gcd.fs b/gcd.fs
new file mode 100644
index 0000000..1baeda0
--- /dev/null
+++ b/gcd.fs
@@ -0,0 +1,37 @@
+: gcd ( a b -- d )
+ 2dup > if swap endif
+ over mod
+ dup 0 <> if recurse else drop endif
+ ;
+
+: gcdn ( a1 a2 .. an n -- d )
+ dup 1 >
+ if
+ 1 - rot rot
+ gcd
+ swap
+ recurse
+ else
+ drop
+ endif
+ ;
+
+
+\ This is gforth-specific.
+\ Usage:
+\ # gforth ./gcd.fs 11 22 33 121
+\ 11
+
+: main
+ 0 >r
+ begin
+ next-arg 2dup 0 0 d<> while
+ s>unumber? if drop else abort endif
+ r> 1 + >r
+ repeat
+ 2drop
+ r> gcdn . cr
+ ;
+
+main bye
+