Write a function that computes the sum of all proper divisors of a given number lisp

(defun proper-divisors-recursive (product &optional (results '(1)))   "(int,list)->list::Function to find all proper divisors of a +ve integer."    (defun smallest-divisor (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 pd-rec (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 (smallest-divisor fac))))            (pd-rec hifac)            (pd-rec (/ product hifac)))))    (pd-rec product)   (butlast (sort (copy-list results) #'<))) (defun task (method &optional (n 1) (most-pds '(0)))   (dotimes (i 19999)      (let ((npds (length (funcall method (incf n))))            (hiest (car most-pds)))         (when (>= npds hiest)            (if (> npds hiest)                (setf most-pds (list npds (list n)))                (setf most-pds (list npds (cons n (second most-pds))))))))   most-pds) (defun main ()   (format t "Task 1:Proper Divisors of [1,10]:~%")   (dotimes (i 10) (format t "~A:~A~%" (1+ i) (proper-divisors-recursive (1+ i))))   (format t "Task 2:Count & list of numbers <=20,000 with the most Proper Divisors:~%~A~%"           (task #'proper-divisors-recursive)))

Are there any code examples left?
New code examples in category Lisp
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Pleased to see you again

Sign up to unlock all of IQCode features:
  • Master useful skills
  • Improve learning outcomes
  • Share your knowledge
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Master useful skills
  • Improve learning outcomes
  • Share your knowledge
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source