当前位置: 首页 > news >正文

sicp每日一题[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 = x 1 − x 2 3 − x 2 5 − . . . \tan x = \frac{x}{1-\frac{x^2}{3-\frac{x^2}{5-...}}} tanx=135...x2x2x

where x x x is in radians. Define a procedure ( t a n − c f x k ) (tan-cf\ x\ k) (tancf x k) that computes an approximation to the tangent function based on Lambert’s formula. k k k specifies the number of terms to compute, as in Exercise 1.37.


1.38难度不大,再加上今天周末,时间充足,我决定再做一道😀
1.39乍一看,并不很难,但是我想了半天也没做出来。直到我忽然发现

tan ⁡ x = x 1 − x 2 3 − x 2 5 − . . . \tan x = \frac{x}{1-\frac{x^2}{3-\frac{x^2}{5-...}}} tanx=135...x2x2x 其实等价于 x 2 1 − x 2 3 − x 2 5 − . . . ÷ x \frac{x^2}{1-\frac{x^2}{3-\frac{x^2}{5-...}}} \div x 135...x2x2x2÷x

然后题目就变得简单了,只要把最后的结果除以 x x x 即可

 ; 注意 n 和 d 都是 procedure 而不是数字,k 表示要计算的项数
(define (cont-frac n d radians k); iterative implementation(define (frac-iter i pre)(if (= i 1)pre; 这里要注意计算的是 (d (- i 1)),而不是 (d i),否则会漏掉 1-... 这一项(frac-iter (- i 1) (/ (n i) (- (d (- i 1)) pre)))))      ; recurative implementation(define (frac-recur i)(if (= i k)(/ (n i) (d i))(/ (n i) (- (d i) (frac-recur (+ i 1))))))(frac-iter k (square radians)))         ; 注意初始值是 x^2;(frac-recur 1)); 注意到原式最上面的分子其实可以写成 x^2 / x,这样它的结构就可以保持一致,只要在最后把结果除以 x 即可
(define (tan-cf x k)(let ((radians (/ (* pi x) 180)))(/ (cont-frac (lambda (i) (square radians))(lambda (i) (- (* 2 i) 1))radiansk)radians))); tan 30° ≈ 0.577,tan 45° = 1,tan 60° ≈ 1.732
(tan-cf 30 10)
(tan-cf 45 10)
(tan-cf 60 10); result
0.5773502691896257
1.0
1.7320508075688845

http://www.mrgr.cn/news/16379.html

相关文章:

  • 自建一款开源音乐服务-Navidrome
  • [YM]课设-C#-WebApi-Vue-员工管理系统 (五)登录
  • 二叉树的相关oj题目 — java实现
  • 最短路算法详解(Dijkstra 算法,Bellman-Ford 算法,Floyd-Warshall 算法)
  • 黄力医生谈健康:掌握这几个秘诀,帮你远离冠心病困扰!
  • Java技术栈 —— Spark入门(三)之实时视频流
  • 算法训练第28天|509. 斐波那契数|70. 爬楼梯|746. 使用最小花费爬楼梯
  • 蜂鸣器奏乐
  • 代码随想录算法训练营第五十九天 | 图论part09
  • nacos获取服务实例流程
  • MP条件构造器之常用功能详解(or、and、exists、notExists)
  • Python 高级特效 - 生成器 ( Generator)
  • DAY59-图论-Bellman_ford
  • HCIP笔记12-交换(1)
  • cnocr 安装
  • Web开发 Ajax 2024/3/31
  • 【C++题解】1722 - 输出两位的巧数
  • 内存管理篇-16二级页表工作原理
  • 揭秘!糖尿病:从绝望到希望的治愈之路
  • Java高级Day34-流补充