(require picturing-programs)
(define (sum n) (/ (* n (+ n 1)) 2))
(define (vol n)
(cond
[(equal? n 1) 1]
[else (+ (sum n) (vol (- n 1)))]))
It does feel a bit like cheating and I do still plan on trying to think of a single expression for the volume. It is neat to see so easily that the 'volume' of one of these pyramids with a bottom side of n=200 is 1,353,400 bottle caps.
Theo
Very cool. You have really "got" recursion when you can apply it to new problems yourself.
ReplyDeleteBy the way, your sum function could be written recursively, if you didn't know the closed form...