summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgor Pashev <pashev.igor@gmail.com>2017-07-02 10:26:35 +0300
committerIgor Pashev <pashev.igor@gmail.com>2017-07-02 10:26:35 +0300
commitde9d2dd966a0e9f9de5c36f5578487cd4deb5a9f (patch)
treeaffdef8629d8d680440e615b1e51df4d909a9a4c
parentd0577298fd2be72ec4e51ae4703ec563e63119a6 (diff)
downloadgcd-de9d2dd966a0e9f9de5c36f5578487cd4deb5a9f.tar.gz
Add Forth
-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
+