summaryrefslogtreecommitdiff
path: root/problem-16.scm
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2015-10-01 05:27:03 -0400
committerDavid Thompson <dthompson2@worcester.edu>2015-10-01 05:27:03 -0400
commitda5a014eb2fa238bff7ffeb76462f4f3114098d0 (patch)
tree916c511bf3796ad0a59969ccebe98f2486eda0a1 /problem-16.scm
parenteff835145de31c77f4d444c4548c1c0f170dc09f (diff)
Add Problem 16 solution.HEADmaster
Diffstat (limited to 'problem-16.scm')
-rw-r--r--problem-16.scm22
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))