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 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. 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/y3 converge. On the other hand, if we average damp twice (i.e., use the average damp of the average damp of y->x/y3) the fixed-point search does converge. Do some experiments to determine how many average damps are required to compute nth roots as a fixed point search based upon repeated average damping of y->x/y^(n-1). Use this to implement a simple procedure for computing nth roots using fixed-point, average-damp,and the repeated procedure of Exercise1.43. Assume that any arithmetic operations you need are available as primitives.


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

; 求 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

sicp每日一题[1.45]的更多相关文章

  1. 老男孩IT教育-每日一题汇总

    老男孩IT教育-每日一题汇总 第几天 第几周 日期 快速访问链接 第123天 第二十五周 2017年8月25日 出现Swap file….already exists以下错误如何解决? 第122天 2 ...

  2. 【Java每日一题】20170106

    20170105问题解析请点击今日问题下方的"[Java每日一题]20170106"查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; ...

  3. 【Java每日一题】20170105

    20170104问题解析请点击今日问题下方的"[Java每日一题]20170105"查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; ...

  4. 【Java每日一题】20170104

    20170103问题解析请点击今日问题下方的"[Java每日一题]20170104"查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; ...

  5. 【Java每日一题】20170103

    20161230问题解析请点击今日问题下方的"[Java每日一题]20170103"查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; ...

  6. 【Java每日一题】20161230

    // 20161229问题解析请点击今日问题下方的"[Java每日一题]20161230"查看(问题解析在公众号首发,公众号ID:weknow619)package Dec2016 ...

  7. 【Java每日一题】20161229

    package Dec2016; import java.util.ArrayList; import java.util.List; public class Ques1229 { public s ...

  8. 【Java每日一题】20161228

    package Dec2016; import java.util.ArrayList; import java.util.List; public class Ques1228 { public s ...

  9. 【Java每日一题】20161227

    package Dec2016; public class Ques1227 { public static void main(String[] args){ } { c = 1; } int c ...

  10. 【Java每日一题】20161226

    package Dec2016; public class Ques1226 { static{ num = 1; } public static int num = 2; public static ...

随机推荐

  1. VulnHub_DC-1渗透流程

    DC-1 DC-1 是一个专门建造的易受攻击的实验室,目的是在渗透测试领域获得经验. 它旨在为初学者提供挑战,但它的难易程度取决于您的技能和知识,以及您的学习能力. 要成功完成此挑战,您将需要 Lin ...

  2. Java 自定义注解校验字段唯一性

    业务场景 在项目中,某些情景下我们需要验证编码是否重复,账号是否重复,身份证号是否重复等... 那么有没有办法可以解决这类似的重复代码量呢? 我们可以通过自定义注解校验的方式去实现,在实体类上面加上自 ...

  3. CF580C

    说句实话,这道题作为蓝题过于简单了一点 #include<iostream> #include<utility> #include<vector> using na ...

  4. 1. C++ 开发环境

    C++ 开发环境 Visual C++ / GCC(G++) / Clang(Clang++) 集成开发环境:Visual Studio / CodeLite / Code::blocks / CLi ...

  5. 基于协同过滤技术的网上书城设计实现(源码+lw+部署文档+讲解等)

    \n文末获取源码联系 感兴趣的可以先收藏起来,大家在毕设选题,项目以及论文编写等相关问题都可以给我加好友咨询 系统介绍: 社会发展日新月异,用计算机应用实现数据管理功能已经算是很完善的了,但是随着移动 ...

  6. 使用uWSGI+nginx部署Django项目(Ubuntu)

    对于uwsgi+nginx的部署方式,它的访问关系大概是: 1 the web client <-> the web server <-> the socket <-&g ...

  7. layui下拉框的数据如何直接从数据库提取(动态赋值)

    代码说明部分 第一步:先把layui官方给的模板粘到自己的前端注:下面的代码是我直接从layui官网粘过来的 <div class="layui-form-item"> ...

  8. Jmeter函数助手4-RandomDate

    RandomDate函数用于生成一段时间范围内的随机日期(年月日). Format string for DateTimeFormatter (optional) (default yyyy-MM-d ...

  9. 自然语言处理:通过API调用各大公司的机器翻译开放平台

    国内大公司做机器翻译做的比较好的有讯飞和百度,这里给出这两个公司机器翻译的开放平台API的介绍: 讯飞开放平台: 链接:https://www.xfyun.cn/doc/nlp/xftrans_new ...

  10. MindSpore 框架的官方预训练模型的加载 —— MindSpore / hub 的安装

    MindSpore计算框架提供了一个官方版本的预训练模型存储库,或者叫做官方版本的预训练模型中心库,那就是 MindSpore / hub . 首先我们需要明确概念: 第一个就是 mindspore_ ...