racket - Recursive procedure for sum of squares of first n odd numbers? -
i'm trying implement recursive procedure sum of squares of first n odd numbers on racket (starting 1) e.g., (sum-alt-squares-recursive 0) 0 (sum-alt-squares-recursive 1) 1 (1^2) (sum-alt-squares-recursive 2) 10 (3^2 + 1^2) (sum-alt-squares-recursive 3) 35 (5^2 + 3^2 + 1^2)
this recursive procedure uses linear iterative process
(define (sum-alt-squares-recursive x (y 1) (z 0)) (if (zero? x) z ; if x zero, return accumulator z (sum-alt-squares-recursive (- x 1) ; x goes down 1 each iteration (+ y 2) ; y starts @ 1 , goes 2 each iteration (+ z (* y y)) ; z, goes y^2 each time ))) (sum-alt-squares-recursive 1) ; => 1 (sum-alt-squares-recursive 2) ; => 10 (sum-alt-squares-recursive 3) ; => 35
Comments
Post a Comment