diff options
author | David Thompson <dthompson2@worcester.edu> | 2015-10-01 05:27:03 -0400 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2015-10-01 05:27:03 -0400 |
commit | da5a014eb2fa238bff7ffeb76462f4f3114098d0 (patch) | |
tree | 916c511bf3796ad0a59969ccebe98f2486eda0a1 /problem-16.scm | |
parent | eff835145de31c77f4d444c4548c1c0f170dc09f (diff) |
Diffstat (limited to 'problem-16.scm')
-rw-r--r-- | problem-16.scm | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/problem-16.scm b/problem-16.scm new file mode 100644 index 0000000..5797b0c --- /dev/null +++ b/problem-16.scm @@ -0,0 +1,22 @@ +;;; +;;; Problem 16: Power digit sum +;;; + +;;; https://projecteuler.net/problem=16 + +(define (digit-sum n) + (let loop ((m 1) + (sum 0)) + (if (> (expt 10 (1- m)) n) + sum + (loop (1+ m) (+ sum (digit m n)))))) + +(define (digit m n) + "Return the Mth digit of N." + ;; Tenths place, hundreds place, etc. + (let ((place (expt 10 (1- m)))) + (/ (- (modulo n (expt 10 m)) + (modulo n place)) + place))) + +(digit-sum (expt 2 1000)) |