summaryrefslogtreecommitdiff
path: root/problem-16.scm
blob: 5797b0c64d1173829b24dcc67e8526ffed6f8abf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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))