首页 > 其他分享 >sicp每日一题[1.45]

sicp每日一题[1.45]

时间:2024-09-05 14:04:24浏览次数:4  
标签:fixed point average sicp nth 1.45 damp 每日 roots

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

标签:fixed,point,average,sicp,nth,1.45,damp,每日,roots
From: https://www.cnblogs.com/think2times/p/18398300

相关文章

  • 每日OJ_牛客_最长递增子序列(dp/贪心模板)
    目录牛客_最长递增子序列(dp/贪心模板)解析代码牛客_最长递增子序列(dp/贪心模板)最长公共子序列__牛客网解析代码在一个序列中找最长递增子序列,动态规划的典型应用,下面是两个模版CISdp模板:#include<iostream>#include<vector>usingnamespacestd;intLIS(vect......
  • 每日搜索论坛总结:2024年8月30日
    以下是今天在搜索论坛上发生的事件回顾,通过搜索引擎圆桌会议和其他网络搜索论坛的视角。Yelp起诉了Google,搜索营销行业对此感到好笑。Google为GoogleAds推出了新标签诊断和同意管理设置。Google表示不会在页面上计数文字或链接。Google正在统一Google商务资料和Google本地服......
  • 软设每日打卡——已知字符集{ a, b, c, d, e, f },若各字符出现的次数分别为{ 6, 3, 8,
    【题目】已知字符集{a,b,c,d,e,f},若各字符出现的次数分别为{6,3,8,2,10,4},则对应字符集中各字符的哈夫曼编码可能为( )        A.00,1011,01,1010,11,100        B.11,100,110,000,0010,01        C.10,1011,11,001......
  • sicp每日一题[1.44]
    Exercise1.44Theideaofsmoothingafunctionisanimportantconceptinsignalprocessing.Iffisafunctionanddxissomesmallnumber,thenthesmoothedversionoffisthefunctionwhosevalueatapointxistheaverageoff(x-dx),f(x),andf(x+......
  • 每日一题 背包,dp,兵营力量训练
    首先,读完这题我一开始有点懵,分析了条件后还是不知道怎么分配比较完美,一开始想一直给最小的那个分配呗,但这不知道分配的力量是多少,没有一个界线,所以要找一个界线,最后还是看了别人的参考答案,用二分确定会变的界线,然后bool数组检查有没有达到界线,没达到的都分配力量,分配的力量......
  • 每日OJ_牛客_最长公共子序列(dp模板)
    目录牛客_最长公共子序列(dp模板)解析代码牛客_最长公共子序列(dp模板)最长公共子序列__牛客网解析代码子序列即两个字符串中公共的字符,但不一定连续。        从题干中可以提取出问题:求字符串s和t的最长公共子序列假设LCS(m,n)为长度为m的字符串s与长度为n的......
  • 软设每日一练10——某文件系统在磁盘上建立了位示图(bitmap),记录磁盘的使用情况。
    【题目】某文件管理系统在磁盘上建立了位示图(bitmap),记录磁盘的使用情况。若计算系统的字长为32位,磁盘的容量为300GB,物理块的大小为4MB,那么位示图的大小需要(      )个字。        A.1200    B.2400    C.6400    D.9600      ......
  • sicp每日一题[1.43]
    Exercise1.43Iffisanumericalfunctionandnisapositiveinteger,thenwecanformthenthrepeatedapplicationoff,whichisdefinedtobethefunctionwhosevalueatxisf(f(...(f(x))...)).Forexample,iffisthefunctionx->x+1,......
  • 2024.9.2 Python,用栈写每日温度,等差数列划分,子串所有可能性,等差数列划分,深度优先搜索
    1.每日温度给定一个整数数组temperatures,表示每天的温度,返回一个数组answer,其中answer[i]是指对于第i天,下一个更高温度出现在几天后。如果气温在这之后都不会升高,请在该位置用0来代替。示例1:输入:temperatures=[73,74,75,71,69,72,76,73]输出:[1,1,4,2,......
  • 【算法每日一练及解题思路】多种方式判断指定字符串其是否为回文字符串
    一、题目:给定一个字符串,判断其是否为回文字符串:二、举例:回文字符串是指一个字符串正读和反读都一样的字符串。例如,“level”、“radar”和“noon”都是回文字符串,因为它们从前往后读和从后往前读都是一样的在Java中,有多种方法可以判断一个字符串是否为回文字符串。以下是......