zoukankan      html  css  js  c++  java
  • SICP 1.7-1.8 solution (Scheme)

    SICP 1.7

    求平方根,要求更加精确

    Exercise 1.7. The good-enough? test used in computing square roots will not be very effective for finding the square roots of very small numbers. Also, in real computers, arithmetic operations are almost always performed with limited precision. This makes our test inadequate for very large numbers. Explain these statements, with examples showing how the test fails for small and large numbers. An alternative strategy for implementing good-enough? is to watch how guess changes from one iteration to the next and to stop when the change is a very small fraction of the guess. Design a square-root procedure that uses this kind of end test. Does this work better for small and large numbers?

     

     

    #lang racket
    ;;SICP 1.7
     
    (define(square x)
      (* x x))
    (define (sqrt-filter guess x)
      (if (newgood-enough? guess (import guess x))
          (import guess x)
          (sqrt-filter (import guess x)
                       x)))
    ;;define improve
    (define (import guess x)
      (average guess (/ x guess)))
     
    (define (average x y)
      (/ (+ x y) 2))
    ;;define good-enough
    (define (good-enough? guess x)
      (< (abs (- (square guess) x)) 0.001))
     
    ;;define new good-enough
    (define (newgood-enough? old-guess new-guess)
      (> 0.01
         (/ (abs (- new-guess old-guess))
            old-guess)))
    ;;test1
    ;;(sqrt-filter 1.0 20)
    (define (sqrt x)
      (sqrt-filter 1.0 x))
    ;test
     
    (sqrt 16)

     SICP 1.8

    首先,将题目给定的算式 x/y2+2y3

     

    求立方根:

    Newton's method for cube roots is based on the fact that if y is an approximation to the cube root of x, then a better approximation is given by the value Use this formula to implement a  cube-root procedure analogous to the square-root procedure. (In section 1.3.4 we will see how to implement Newton's method in general as an abstraction of these square- root and cube-root procedures.)

     

    #lang racket
    ;;SICP test 1.8
     
    (define(square x)
      (* x x))
    (define (sqrt-filter guess x)
      (if (good-enough? guess x)
          guess
          (sqrt-filter (improve guess x)
                       x)))
    ;;define good-enough
    (define (good-enough? guess x)
      (< (abs (- (club guess) x)) 0.001))
    ;;define improve
    (define (improve guess x)
      (/ (+ (/ x (square guess)) (* 2 guess))
         3))
    ;;define club
    (define (club x)
      (* x x x))
    ;;test1
    ;;(sqrt-filter 1.0 20)
    (define (sqrt x)
      (sqrt-filter 1.0 x))
    ;test
    (sqrt 8)

    (Scheme实现)

     

  • 相关阅读:
    _00020 妳那伊抹微笑_谁的异常最诡异第一期之 SqlServer RSA premaster secret error
    &lt;&lt;Python基础教程&gt;&gt;学习笔记 | 第12章 | 图形用户界面
    ubuntu 14.04 桌面版关闭图形界面
    (一)简单工厂模式
    JS学习笔记-数据类型
    【C#】报表制作&lt;机房重构&gt;
    [leetcode][math] Add Digits
    hibernate(三) 一对多映射关系
    hibernate(二)一级缓存和三种状态解析
    Hibernate(五)之一对多&多对一映射关系
  • 原文地址:https://www.cnblogs.com/pengjunwei/p/4219971.html
Copyright © 2011-2022 走看看