(define best-pi-fourth (atan 1)) (define leibniz-term (lambda (i) ;;; this procedure takes one argument, an integer, and returns the value of ;;; the ith term in the Leibniz approximation (/ (expt -1.0 (- i 1)) (- (* 2 i) 1)))) (define pi-approx-leibniz-iterative (lambda (n) ;; this procedure takes one argument, an integer, and returns the value of ;; the sum of the first n (starting from 1) terms of the leibniz approximation (define helper (lambda (i sum) ;; an internal procedure to sum the n terms ;; i is the index of the next term to add ;; sum is the current sum of the previous terms (if (= i n) ; test to seeif we are done sum ; if so return the sum (helper (+ i 1) ; otherwise go on to the next term (+ sum (leibniz-term (+ i 1))))))) ; while adding this term to the sum ;; we need to start the process with the first term (helper 1 (leibniz-term 1)))) (define pi-approx-leibniz-recursive (lambda (n) ;; this procedure takes one argument, an integer, and returns the value of ;; the sum of the first n (starting from 1) terms of the leibniz approximation ;; in this case the procedure will count down from the nth term (if (= n 1) ; test to see if done 1 ; if so return the value of the 1st term (+ (leibniz-term n) ; otherwise add in the value of nth term (pi-approx-leibniz-recursive (- n 1)))))) ; to the value of the previous n-1 terms