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 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
相关推荐
RlQIbaGC8 小时前
基于DBSCAN密度聚类的风电-负荷场景削减方法 关键词:密度聚类 场景削减 DBSCAN 场...
lisp
Tiger Z4 天前
《R for Data Science (2e)》免费中文翻译 (第19章) --- Joins(1)
r语言·编程·数据科学
xp47580638010 天前
陪诊公司是什么?绿通在北京陪诊服务中的作用是什么?
游戏·编程
酬勤-人间道10 天前
XPlote3DGenie 2.1.1.0:实用 3D 数据处理工具,百度网盘可直接安装
c++·3d·gis·编程·计算机软件·岩土
十年编程老舅11 天前
虾皮C++一面:C++四种类型转换详解
程序员·编程·c/c++
xp47580638012 天前
当选择北京陪诊公司时,如何找到靠谱的陪诊服务?
游戏·编程
程序员鱼皮14 天前
20 个神级 AI 编程扩展,爽爆了!
ai·程序员·编程
开开心心_Every14 天前
文件数量统计工具:支持多层文件夹数量统计
游戏·微信·pdf·excel·语音识别·swift·lisp
王老师青少年编程18 天前
2025年3月GESP真题及题解(C++七级): 等价消除
c++·编程·题解·真题·gesp·七级·等价消除
王老师青少年编程18 天前
2025年3月GESP真题及题解(C++八级): 上学
c++·编程·题解·真题·gesp·八级·上学