; needed: ; get input til negative, print max ; is perfect, and perfect looper ; 3n+1 problem (defun stupid-max (&optional (max 0)) (progn (format t "Enter a number, stupid: ") (let ((n (read))) (if (< n 0) max (stupid-max (if (> n max) n max)))))) (defun idiot-max (&optional (n 1) (lst ())) (if (< n 0) (reduce #'max lst) ;(car (sort lst #'>)) (progn (format t "Enter a number, fool: ") (let ((x (read))) (idiot-max x (append lst (list x))))))) ; usable: (LEN RND); (length return) = LEN, (reduce #'max return) < RND (defun random-list (len &optional (rnd 100) (cur 0) (lst ())) (if (= len cur) lst (random-list len rnd (1+ cur) (append lst (list (random rnd)))))) (defun perfect (n) (if (= n (reduce #'+ (ffactor n))) t nil)) (defun ffactor (n &optional (i 1) (fac ())) ;then this came 2nd (if (< n 2) (list 1) (if (= n i) fac (ffactor n (1+ i) (if (zerop (mod n i)) (append fac (list i)) fac))))) (defun factors (n) ;this came first (mapcar #'(lambda (x) (if (zerop (mod n x)) x)) (range 1 n))) (defun rrange (max &optional (min 0) (lst '(0))) (if (null lst) (rrange max '(0)) (if (= (1+ (car (last lst))) max) lst (rrange max (append lst (list (1+ (car (last lst))))))))) (defun arange (max &optional (min 0) (lst '(0))) (cond ((= min max) (list min)) ((> min max) (arange (1+ min) max (list max))) (t (if (= (1+ (car (last lst))) max) lst (arange max min (append lst (list (1+ (car (last lst)))))))))) ;pythonesque range() with benefits; ends with provided end, goes both ways,... (defun range (start &optional end lst) (if (null end) (range 0 start '(0)) (if (null lst) (range start end (list start)) (if (= (car (last lst)) end) lst (range start end (append lst (list (+ (if (minusp (- start end)) 1 -1) (car (last lst)))))))))) (defun 3nloop (a &optional b) (cond ((equal a b) (format t "~A: ~S~%" b (3n b))) ((null b) (3nloop 2 a)) (t (format t "~A: ~S~%" a (3n a)) (3nloop (1+ a) b)))) ; sort list of 2-tuple lists by the 2nd tuple (defun 2sortl (lst) (sort lst #'(lambda (a b) (< (cadr a) (cadr b))))) (defun 2sortg (lst) (sort lst #'(lambda (a b) (> (cadr a) (cadr b))))) (defun 3nmax2 (a b) (car (2sortg (3nlen a b)))) (defun 3nmax (a b) (reduce #'max (mapcar #'cadr (3nlen a b)))) (defun 3nlen-print (lst) (if (null lst) t (progn (format t "N(~A) = ~A~%" (caar lst) (cadar lst)) (3nlen-print (cdr lst))))) (defun 3nlen (a b &optional ls) (if (= a (1+ b)) ls (3nlen (1+ a) b (append ls (list (list a (length (3n a)))))))) (defun 3n (n &optional ns) (if (= n 1) (append ns (list 1)) (3n (if (evenp n) (/ n 2) (+ 1 (* 3 n))) (append ns (list n))))) (defun 5n (n &optional ns) (if (= n 1) (append ns (list 1)) (3n (if (evenp n) (/ n 2) (+ 1 (* 5 n))) (append ns (list n)))))