summaryrefslogtreecommitdiff
path: root/number-spiral-diagonals/nsd.scm
diff options
context:
space:
mode:
authorbd <bdunahu@operationnull.com>2024-08-01 22:37:18 -0600
committerbd <bdunahu@operationnull.com>2024-08-01 22:37:18 -0600
commit64fcac482de0ba1a401cfc42066196a38661405e (patch)
treec8760685a6f28b12dedd08c00abc44ea7b731b26 /number-spiral-diagonals/nsd.scm
parent7cbaa3cfc3691147c28614783c3fc3ebfdc0b042 (diff)
Closure, coin sums, & number spiral generator
Diffstat (limited to 'number-spiral-diagonals/nsd.scm')
-rw-r--r--number-spiral-diagonals/nsd.scm27
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)))