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
相关推荐
窗户9 天前
排列和组合的实现
排列组合·函数式·haskell·scheme
络~11 天前
shell编程-for循环-while循环-until循环基础
编程
网络安全成叔11 天前
【入门级】计算机网络学习
学习·计算机网络·web安全·计算机·网络安全·编程
skywalk816313 天前
易于使用和学习的编程语言Arc(list)入门手册
list·编程·arc
Javatutouhouduan1 个月前
如何系统全面地自学Java语言?
java·后端·程序员·编程·架构师·自学·java八股文
程序员鱼皮1 个月前
一句话,我让 AI 帮我做了个 P 图网站!
计算机·ai·编程·开发
WANGWUSAN661 个月前
Python高频写法总结!
java·linux·开发语言·数据库·经验分享·python·编程
程序无涯海1 个月前
【Java技巧】深入浅出 Guava Retry 框架:业务兜底重试方案示例
java·开发语言·编程·guava·重试
hjxxlsx1 个月前
利用编程获得money?
编程·指南·赚钱
程序员鱼皮1 个月前
离谱!学编程两年,还不会用工具类?
后端·计算机·编程·开发·求职