Skip to content

Instantly share code, notes, and snippets.

@mrklein
Created September 26, 2012 23:23
Show Gist options
  • Save mrklein/3791253 to your computer and use it in GitHub Desktop.
Save mrklein/3791253 to your computer and use it in GitHub Desktop.
Summing digits of factorials
(defun factorial (n)
(defun i_factorial (k acc)
(if (= 0 k)
acc
(i_factorial (- k 1) (* k acc))))
(i_factorial n 1))
(defun digits (n)
(defun i_digits (k acc)
(if (= 0 k)
acc
(i_digits (truncate k 10) (cons (mod k 10) acc))))
(i_digits n '()))
(defun digits_s (n)
(mapcar
#'(lambda (k) (parse-integer (string k)))
(coerce (write-to-string n) 'list)))
(format T "~D~%"
(time (reduce
'+
(mapcar
#'(lambda (n)
(reduce
'+
(digits_s (factorial n))))
'(1 12 123 1234 12345 123456)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment