asb: head /dev/brain > /dev/www

My home, musings, and wanderings on the world wide web.

99 LISP problems: Problem #34

Solution to the 99 LISP Problems #34

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(defun range (lo hi)
    (cond ((> lo hi) nil)
    (t (cons lo (range (1+ lo) hi)))))

(defun eucgcd (m n)
    (let ((p (abs m)) (q (abs n)))
        (cond ((= m n) m)
        (t (gcd (- (max m n) (min m n)) (min m n))))))

(defun coprimep (m n)
    (= (eucgcd m n) 1))

(defun totient (m)
  (cond ((= m 1) 1)
        (t (apply #'+ (mapcar
                        (lambda (x) (if (coprimep m x) 1 0))
                        (range 1 (1- m)))))))

Lisp dialect: Steel Bank Common Lisp