Write a function that computes the sum of all proper divisors of a given number lisp
(defun properdivisorsrecursive (product &optional (results '(1))) "(int,list)>list::Function to find all proper divisors of a +ve integer." (defun smallestdivisor (x) "int>int::Find the smallest divisor of an integer > 1." (if (evenp x) 2 (do ((lim (truncate (sqrt x))) (sd 3 (+ sd 2))) ((or (integerp (/ x sd)) (> sd lim)) (if (> sd lim) x sd))))) (defun pdrec (fac) "(int,int)>nil::Recursive function to find proper divisors of a +ve integer" (when (not (member fac results)) (push fac results) (let ((hifac (/ fac (smallestdivisor fac)))) (pdrec hifac) (pdrec (/ product hifac))))) (pdrec product) (butlast (sort (copylist results) #'<))) (defun task (method &optional (n 1) (mostpds '(0))) (dotimes (i 19999) (let ((npds (length (funcall method (incf n)))) (hiest (car mostpds))) (when (>= npds hiest) (if (> npds hiest) (setf mostpds (list npds (list n))) (setf mostpds (list npds (cons n (second mostpds)))))))) mostpds) (defun main () (format t "Task 1:Proper Divisors of [1,10]:~%") (dotimes (i 10) (format t "~A:~A~%" (1+ i) (properdivisorsrecursive (1+ i)))) (format t "Task 2:Count & list of numbers <=20,000 with the most Proper Divisors:~%~A~%" (task #'properdivisorsrecursive)))
Are there any code examples left?
New code examples in category Lisp

Lisp 20211114 12:24:23 Doom emacs pdftools

Lisp 20210928 03:48:03 common lisp map number to word

Lisp 20210918 06:32:03 common lisp ide macos

Lisp 20210914 16:17:02 for loop in common lisp