summaryrefslogtreecommitdiff
path: root/gcd.fs
blob: 1baeda074655644cea09235707be76457670eaf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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