From da5a014eb2fa238bff7ffeb76462f4f3114098d0 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 1 Oct 2015 05:27:03 -0400 Subject: Add Problem 16 solution. --- problem-16.scm | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 problem-16.scm (limited to 'problem-16.scm') 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)) -- cgit v1.2.3