summaryrefslogtreecommitdiff
path: root/gcd.lisp
blob: 8899491b88df12b8da4290e194d30f44abad4ae7 (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
; SYNOPSIS:
; # clisp gcd.lisp 11 22 33 121
; # ecl --shell gcd.lisp  121 22 33
; # gcl -f gcd.lisp 121 22 33
; # sbcl --script gcd.lisp 121 22 33

(defun gcd2 (a b)
  (loop while (/= b 0) do
    (psetq a b b (mod a b))
    finally (return a)))

(defun gcdn (n &rest ns)
  (reduce #'gcd2 ns :initial-value n))

(defun program-args ()
  (or
   #+CLISP *args*
   #+ECL   (cdr ext:*unprocessed-ecl-command-args*)
   #+GCL   (cdr si::*command-args*)
   #+SBCL  (cdr *posix-argv*)
   nil))

(defun numbers ()
  (mapcar #'parse-integer (program-args)))

(let ((ns (numbers)))
  (when ns
    (write (apply #'gcdn ns))
    (fresh-line)))