summaryrefslogtreecommitdiff
path: root/gcd.lisp
blob: 79852a9d48300d9dea48a98b1388dbabf1add757 (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
; 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 (ext:command-args)
   #+GCL si::*command-args*
   #+SBCL *posix-argv*
   nil))

(defun numbers ()
  (remove nil
          (map 'list (lambda (x) (parse-integer x :junk-allowed t))
               (program-args))))

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