December 30, 2008
at
Tuesday, December 30, 2008
Labels:
Computer Science,
Lisp,
Project Euler
Posted by
Billy
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)))
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment