summaryrefslogtreecommitdiff
path: root/problem-16.scm
diff options
context:
space:
mode:
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))