I am trying to create a function `prime-factors` that returns the prime factors of a number. To do so, I created `is-prime` function, and `prime-factors-helper` that will do a recursive check of the prime factors.

```
(defun is-prime (n &optional (d (- n 1))) 
  (if (/= n 1) (or (= d 1)
          (and (/= (rem n d) 0)
               (is-prime  n (- d 1)))) ()))

(defun prime-factors-helper (x n)
   (if (is-prime x) 
       (list x) 
       (if (is-prime n) 
            (if (AND (= (mod x n) 0) (<= n (/ x 2)))
                (cons n (prime-factors-helper (/ x n) n))
                (prime-factors-helper x (+ 1 n)))       
            (prime-factors-helper x (+ 1 n)))))

(defun prime-factors (x)
    (prime-factors-helper x 2)) 

```

I have a problem of optimisation. When I have a big number such as `123456789`, I get this error message "Stack overflow (stack size 261120)". I believe because the correct answer of `(prime-factors 123456789)` is `(3 3 3607 3803)`, my program will take so long to find the next prime factor once it constructs the list with the two first elements `(3 3)`. How can I optimise my code?

```
 CL-USER 53 > (prime-factors 512)
 (2 2 2 2 2 2 2 2 2)

 CL-USER 54 > (prime-factors 123456789)

 Stack overflow (stack size 261120).
   1 (abort) Return to level 0.
   2 Return to top loop level 0.

 Type :b for backtrace or :c <option number> to proceed.
 Type :bug-form "<subject>" for a bug report template or :? for other options

```