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

sicp每日一题[1.45]

Exercise 1.45

We saw in Section 1.3.3 that attempting to compute square roots by naively finding a fixed point of y → x y y\rightarrow \frac{x}{y} yyx does not converge, and that this can be fixed by average damping. The same method works for finding cube roots as fixed points of the average-dampedy x y 2 \frac{x}{y^2} y2x. Unfortunately, the process does not work for fourth roots—a single average damp is not enough to make a fixed-point search for y → x y 3 y\rightarrow \frac{x}{y^3} yy3x converge. On the other hand, if we average damp twice (i.e., use the average damp of the average damp of y → x y 3 y\rightarrow \frac{x}{y^3} yy3x) the fixed-point search does converge. Do some experiments to determine how many average damps are required to compute n t h n^{th} nth roots as a fixed point search based upon repeated average damping of y → x y n − 1 y\rightarrow \frac{x}{y^{n-1}} yyn1x. Use this to implement a simple procedure for computing n t h n^{th} nth roots using f i x e d − p o i n t , a v e r a g e − d a m p fixed-point, average-damp fixedpoint,averagedamp,and the r e p e a t e d repeated repeated procedure of Exercise1.43. Assume that any arithmetic operations you need are available as primitives.


这道题难度太难了,我最后也没能靠自己做出来。一个是怎么找到要执行几次 a v e r a g e − d a m p average-damp averagedamp,我一开始以为是 n − 2 n-2 n2,试了几个发现明显不是,又猜测是不是 n 2 \frac{n}{2} 2n,结果还是不对,最后上网搜了一下才知道是 l o g 2 n log_2^n log2n,感兴趣的可以参考知乎的这个回答;知道了重复执行的次数,在编写代码的时候再次遇到了问题,我对于“把一个过程作为另一个过程的返回值”这个概念理解的还是不到位,没有理解(repeated average-damp n)之后还要给它传一个过程作为 a v e r a g e − d a m p average-damp averagedamp 的参数,最后上网看了别人的答案才明白过来。下面是我的答案:

; 求 x 和 f(x) 的平均值
(define (average-damp f)(lambda (x) (average x (f x)))); 对于任意正整数 n,求使得 2^k < n 的最大 k 值
(define (max-expt n)(define (iter k pre)(if (< n pre)(- k 1)(iter (+ k 1) (* 2 pre))))(iter 1 2))(define (nth-root x n)(fixed-point ((repeated average-damp (max-expt n))(lambda (y) (/ x (expt y (- n 1)))))1.0))(display (nth-root 2 2))
(newline)
(display (nth-root 32 5))
(newline); 结果
1.4142135623746899
2.000001512995761

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • LeetCode 热题100-17 缺失的第一个正数
  • nodejs笔记
  • 聚观早报 | 淘宝将支持微信支付;董明珠支持招35岁员工
  • AE调试一些记录(1)
  • 探索NVIDIA RTX 4060 8G与RTX 3060 12G:性能与适用场景的深度解析
  • 从零开始,认识游戏设计师(3)体验源于设计师①
  • 苹果手机怎么设置铃声?3个方法教你制定个性化铃声
  • 鲁大师8月新机性能/流畅/AI/久用榜:新机节奏放缓,但不乏小惊喜
  • Linux malloc内存分配实现原理
  • 828华为云征文 | Flexus X实例与Harbor私有镜像仓库的完美结合
  • armbian cups 远程打印机 1022
  • 「OC」iOS事件处理流程
  • Vu3 跨组件通讯
  • ASPICE评估前的重要准备事项
  • 【 html+css 绚丽Loading 】000037 六合归一心
  • [ JavaScript ] 数据结构与算法 —— 链表
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • bootstrap创建登录注册页面
  • Docker容器管理
  • JavaScript DOM 10 - 滚动
  • js 实现textarea输入字数提示
  • opencv python Meanshift 和 Camshift
  • swift基础之_对象 实例方法 对象方法。
  • Vue 2.3、2.4 知识点小结
  • XForms - 更强大的Form
  • 测试如何在敏捷团队中工作?
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 从0到1:PostCSS 插件开发最佳实践
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 少走弯路,给Java 1~5 年程序员的建议
  • 用jquery写贪吃蛇
  • hi-nginx-1.3.4编译安装
  • puppet连载22:define用法
  • # 安徽锐锋科技IDMS系统简介
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • # 数仓建模:如何构建主题宽表模型?
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • #14vue3生成表单并跳转到外部地址的方式
  • #大学#套接字
  • #使用清华镜像源 安装/更新 指定版本tensorflow
  • (10)ATF MMU转换表
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (52)只出现一次的数字III
  • (C)一些题4
  • (C++)八皇后问题
  • (zt)最盛行的警世狂言(爆笑)
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (附源码)python房屋租赁管理系统 毕业设计 745613
  • (六)vue-router+UI组件库
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • (转)ABI是什么
  • (转)EXC_BREAKPOINT僵尸错误
  • (转载)hibernate缓存