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
相关推荐
Jay-r16 小时前
手势粒子特效系统 Gesture Particle FX(附源码下载)
python·ai·编程·pygame·百度云·手势控制
大模型momo18 小时前
AI 编程工程化:从 Prompt 到 Harness
人工智能·prompt·编程·agent
码字的特恩2 天前
AI Agent Skill 工程化 09:让 Skill 自己变好——走向自进化流水线
人工智能·计算机·ai·程序员·大模型·互联网·编程
人间凡尔赛3 天前
2026年AI编程新范式:从Copilot到Agentic Coding的实战指南
ai·编程·agent·工具·效率
osbxbv_4713 天前
消息中间件选型对比分析
编程
ygrjcn_0664 天前
Rust的闭包语法展开
编程
osbxbv_4715 天前
HTTP协议简介:看懂浏览器和服务器如何对话
编程
leqjsz_0885 天前
JavaScript的String.prototype.replaceAll:全局替换的性能
编程
大数据点灯人7 天前
【AI编程】Vibe Coding 模型选型:越贵越好吗?效果/速度/成本平衡指南
编程·ai编程·claude·codex·vibe coding
AI小码7 天前
把动作「画」给视频世界模型,跨本体双向推演,李飞飞参与
大数据·人工智能·算法·ai·大模型·音视频·编程