Created
September 26, 2012 23:23
-
-
Save mrklein/3791253 to your computer and use it in GitHub Desktop.
Summing digits of factorials
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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