0 Replies - 1228 Views - Last Post: 17 October 2010 - 07:43 AM

#1 krzysz00   User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 83
  • Joined: 25-February 09

[Lisp] All the factors of a number n

Posted 17 October 2010 - 07:43 AM

Description: Paste the code into a file and (load "file.lisp") at your REPL.The snippet calculates all the factors of a number n that is input at the console
(defun factors (n)
  (loop with list = nil
       with m = most-negative-fixnum
     for i from 1 to (/ n 2) 
     until (= i m) do
       (let ((div (/ n i)))
         (when (typep div 'integer)
           (push i list) (push div list) (setf m div)))
     finally (return list)))

(defun main ()
  (let ((arg nil))
    (loop 
       (format t "Factor which number: ")
       (finish-output)
       (setf arg (parse-integer (read-line) :junk-allowed t))
       (when arg (return)))
    (format t "~{~d and ~d~^, ~}~%" (nreverse (factors arg)))))

(main)



Is This A Good Question/Topic? 0
  • +

Page 1 of 1