>>34
what about lisp?
(defun factor (num factors)
(let ((l 0) (n num))
(loop for i from 2 to (<= i (/ num 2)
do (while (/= 0 (mod n i))
(setf n (/ n i))
(setf (aref factors (incf l)) i)))
(when (= 0 l)
(setf l)
(setf (aref factors 0) num))
l))
then again, there is no real reason to use arrays, so we will use lists. so there is no reason to return the length... the user can easily do (length list)... so we end up with this
(defun factor (n &optional (i 2))
(cond
((> i n) '())
((divisible n i) (cons i (factor (/ n i) i)))
(t (factor n (+ 1 i)))))
[45]> (factor 22)
(2 11)
[46]> (factor 34)
(2 17)
[47]> (factor 40)
(2 2 2 5)
Yes, it is not that efficient. but it's nice and clean, and if you really need to, you can use the above version.