码迷,mamicode.com
首页 > 编程语言 > 详细

Lisp经典算法

时间:2018-03-26 22:26:47      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:post   square   ssi   art   init   logs   body   判断   mat   

求平方根

SUCCESSIVE AVERAGING DUE TO HERON OF ALEXANDRIA
** TO FIND AN APPROXIMATION TO SQRT(X) **

  1. MAKR A GUESS G
  2. IMPROVE THE GUESS BY AVERAGING G AND X/G
  3. KEEP IMPROVING THE GUESS UNTILE IT IS GOOD ENOUGH
  4. USE 1 AS AN INITIAL GUESS

    算法思想

    (define (try guess x)
    (if (good-enough? guess x)
        guess
        (try (improve guess x) x)))
    (define (sqrt x) (try 1 x))

    参考代码

    注:此代码来自https://blog.csdn.net/jazz_charles/article/details/48974029

    (define (square x) (* x x)) //定义求平方  
    (define (average x y) (/ (+ x y) 2)) //定义求平均值  
    (define (sqrt x) //定义求平方根  
      (define (accurate guess) //定义判断精确度  
           (<   (abs (-  (square guess)  x)) 0.001))  
      (define (improve guess) //定义改进近似值  
          (average x  (/ x guess)))  
      (define (sqrt-iter guess) //定义主体迭代求解  
          (if   (accurate guess)  
                 guess  
                (sqrt-iter (improve guess))))  
      (sqrt-iter 1.0))  

Lisp经典算法

标签:post   square   ssi   art   init   logs   body   判断   mat   

原文地址:https://www.cnblogs.com/zhuxiaoxi/p/8654113.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!