-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path28.ss
More file actions
25 lines (21 loc) · 695 Bytes
/
28.ss
File metadata and controls
25 lines (21 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#lang scheme
(require math/number-theory
(planet schematics/schemeunit:3)
(except-in srfi/1 first second)
(lib "26.ss" "srfi"))
;; n is the length of the diagonal, from the center 1 to the corner,
;; counting both of those. So for the spiral from 1 through nine, n
;; is 2.
(define (sum-of-four-corners n)
(if (= 1 n)
1
(let* ((length (sub1 (* 2 n)))
(ur (expt length 2)))
(- (* 4 ur)
(* 6 (sub1 length))))))
(define (diagonal-sum side-length)
(for/fold ([sum 0])
([i (in-range 1 (add1 (/ (add1 side-length) 2)))])
(+ sum (sum-of-four-corners i))))
(check-equal? (diagonal-sum 5) 101)
(diagonal-sum 1001)