So I was not at all happy with my 0.6 second time with the previous iteration. It wasn’t clean, it used two loops and it just wasn’t as smart and beautiful as it could have been. But I have fixed all of that. This program now finds all of the primal roots of any number. And now it finds the answer in 0.004 seconds.
I’ve commented the code to show its beauty.
(defun problem3a (x lst)
”Find the prime factors of any number, input number and blank list ()”
(let ((nextx x))
; we know 1 is a prime 2 to nextx
(loop for i from 2 to nextx
; if i is a factor append it to the list
do (cond ((eq (mod nextx i) 0)
(setq lst (append lst (multiple-value-list i)))
; remove i from the number
(setq nextx (/ nextx i))
; set i back to 1 (i-1 after this loop is done)
(setq i (- i 1))
; if nextx is equal to one you are done, you found all the primes
(cond ((eq 1 nextx)
(return nil))))))
(print lst)))