I am studying SICP and wrote two procedures to compute the sum of 1/n^2, the first generating a recursive process and the second generating an iterative process :
(define (sum-rec a b) (if (> a b) 0 (exact->inexact (+ (/ 1 (* a a)) (sum-rec (1+ a) b))))) (define (sum-it a b) (define (sum_iter a tot) (if (> a b) tot (sum_iter (1+ a) (+ (/ 1 (* a a)) tot)))) (exact->inexact (sum_iter a 0))) I tested that both procedures give exactly the same results when called with small values of b, and that the result is approaching $pi^2/6$ as b gets larger, as expected.
But surprisingly, calling (sum-rec 1 250000) is almost instantaneous whereas calling (sum-it 1 250000) takes forever.
Is there an explanation for that?
sum_iteruses exact arithmetic;sum-recdoesn't.