# HG changeset patch # User Steve Losh # Date 1487336007 0 # Node ID 137ba2e799c49f368aa1c5c5f360270d185150dd # Parent cdd3f0c782573616ab0814fbf4b48b6c3080ce14 Problem 20 diff -r cdd3f0c78257 -r 137ba2e799c4 src/euler.lisp --- a/src/euler.lisp Thu Feb 16 14:59:06 2017 +0000 +++ b/src/euler.lisp Fri Feb 17 12:53:27 2017 +0000 @@ -91,6 +91,11 @@ i)))) +(defun factorial (n) + (iterate (for i :from 1 :to n) + (multiplying i))) + + ;;;; Problems ----------------------------------------------------------------- (defun problem-1 () ;; If we list all the natural numbers below 10 that are multiples of 3 or 5, @@ -537,6 +542,15 @@ local-time:timestamp-day-of-week zerop)))) +(defun problem-20 () + ;; n! means n × (n − 1) × ... × 3 × 2 × 1 + ;; + ;; For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, + ;; and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27. + ;; + ;; Find the sum of the digits in the number 100! + (sum (digits (factorial 100)))) + ;;;; Tests -------------------------------------------------------------------- (def-suite :euler) @@ -561,6 +575,7 @@ (test p17 (is (= 21124 (problem-17)))) (test p18 (is (= 1074 (problem-18)))) (test p19 (is (= 171 (problem-19)))) +(test p20 (is (= 648 (problem-20)))) ;; (run! :euler)