SICP Exercise 1.39
Question
A continued fraction representation of the tangent function was published in 1770 by the German mathematician J.H. Lambert:
where (tan-cf x k)
that computes an approximation to the tangent function based on Lambert’s formula. k
specifies the number of terms to compute, as in exercise 1.37.
Answer
Our expected result for
(define (square-unless-one x)
(if (= x 1) x
(* x x)))
(define (get-n x) (- (* 2 x) 1))
(define (tan-cf x k)
(define (iterate counter)
(if (= k counter) (/ (square-unless-one x) (get-n counter))
(/ (square-unless-one x) (- (get-n counter) (iterate (+ counter 1))))))
(iterate 1))
(tan-cf 1.0 4)
Results:
1.5573770491803278