summaryrefslogtreecommitdiff
path: root/number-spiral-diagonals/nsd.scm
blob: 9d4bfe3dfec45826ac2beaea6ad706feffde308d (plain)
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
26
27
(define-module (nsd)
  #:export (nsd
            nsd-generator
            calculate-seq-length))


(define (nsd n)
  "Given dimension N of a square, calculates and sums
an appropriately sized sequence."
  (apply + (nsd-generator (calculate-seq-length n))))

(define (nsd-generator k)
  "Generates a sequence of numbers corresponding to the
diagonals in a square. See README for more details on how
this is used to solve the problem."
  (let loop ((i 0)
             (lst '(1)))
    (if (>= (1+ i) k)
        (reverse lst)
        (loop (1+ i)
              (cons (+ (car lst) (* 2 (1+ (quotient i 4)))) lst)))))

(define (calculate-seq-length n)
  "Calculates the size of the sequence. This is equal to
the length of the two diagonals, minus one for the overlapping
center."
  (1- (* 2 n)))