SICP - Solution: Exercise 1.39

# SICP - Solution: Exercise 1.39

## Exercise 1.39 #

A continued fraction representation of the tangent function was published in 1770 by the German mathematician J.H. Lambert:

$$\tan x=\frac x{1-{\displaystyle\frac{x^2}{3-{\displaystyle\frac{x^2}{5-\cdots}}}}}$$

where x is in radians. Define a procedure (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.

## Solution #

From what is already done, the solution is:

(define (cont-frac-iter n d k)
(define (iter i result)
(if (= 0 i)
result
(iter (sub1 i) (/ (n i) (+ result (d i))))))
(iter (sub1 k) (/ (n k) (d k))))

(define (tan-cf x k)
(cont-frac-iter
(lambda (i) (if (= i 1) x (* x x -1)))
(lambda (i) (- (* 2.0 i) 1))
k))

; check result
(define x 1)
(display (tan-cf x 8)) (newline)
(display (tan x)) (newline)


Which returns:

1.557407724654856
1.557407724654902
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 1, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}