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
相关推荐
再思即可5 天前
sicp每日一题[2.77]
算法·lisp·函数式编程·sicp·scheme
G皮T5 天前
【设计模式】行为型模式(三):责任链模式、状态模式
java·设计模式·状态模式·编程·责任链模式·state
xingbuxing_py6 天前
精华帖分享|浅谈金融时间序列分析与股价随机游走
python·金融·编程·量化交易·理财·量化投资·股市
JavaPub-rodert6 天前
# Python IDE的介绍和选择 --- 《跟着小王学Python》
开发语言·ide·python·编程·开发
Python_trys12 天前
Python爬虫基础-正则表达式!
开发语言·爬虫·python·正则表达式·编程
howard200512 天前
初试Lisp语言
开发语言·lisp
科技前言17 天前
探索Python编程:从入门到实践的全面指南
编程
再思即可18 天前
sicp每日一题[2.63-2.64]
算法·lisp·函数式编程·sicp·scheme
G皮T18 天前
【设计模式】结构型模式(二):代理模式
java·设计模式·编程·代理模式·proxy pattern·结构型模式
Cici_ovo20 天前
编程相关学习点——代码内容及结构
python·学习·编程·api