(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)))