December 30, 2008

n! means n × (n − 1) × ... × 3 × 2 × 1

Find the sum of the digits in the number 100!


(defun add-digits (num)
"Returns the sum of the number's digits"
(let ((sum 0)
(num-string (write-to-string num)))
(dotimes (i (length num-string))
(setf sum (+ sum (digit-char-p (schar num-string i)))))
sum))

(defun fact (num)
(if (= num 1)
1
(* num (fact (1- num)))))

(defun euler-20 ()
(add-digits (fact 100)))

0 comments: