diff options
Diffstat (limited to 'number-spiral-diagonals/nsd.scm')
-rw-r--r-- | number-spiral-diagonals/nsd.scm | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/number-spiral-diagonals/nsd.scm b/number-spiral-diagonals/nsd.scm new file mode 100644 index 0000000..9d4bfe3 --- /dev/null +++ b/number-spiral-diagonals/nsd.scm @@ -0,0 +1,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))) |