免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: win_hate
打印 上一主题 下一主题

SICP 习题参考答案 1.2  关闭 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2008-09-08 19:24 |只看该作者

Exercise 1.12 答案

Fib(n)=(phi^n-psi^n)/sqrt(5)

容易证明:

1、|psi|<1, 所以 |(psi)^n|<1
2、1/sqrt(5)<1/2

所以 |psi^n/sqrt(5)|<1/2,即 Fib(n) 是最接近 phi^n/sqrt(5) 的整数。

论坛徽章:
0
12 [报告]
发表于 2008-09-08 19:26 |只看该作者

Exercise 1.14

Exercise 1.14.  Draw the tree illustrating the process generated by the count-change procedure of section 1.2.2 in making change for 11 cents. What are the orders of growth of the space and number of steps used by this process as the amount to be changed increases?

论坛徽章:
0
13 [报告]
发表于 2008-09-08 19:28 |只看该作者

Exercise 1.14. 答案

等我素描班毕业后再画。

论坛徽章:
0
14 [报告]
发表于 2008-09-08 19:30 |只看该作者

Exercise 1.15.

Exercise 1.15.  The sine of an angle (specified in radians) can be computed by making use of the approximation sin x x if x is sufficiently small, and the trigonometric identity



to reduce the size of the argument of sin. (For purposes of this exercise an angle is considered ``sufficiently small'' if its magnitude is not greater than 0.1 radians.) These ideas are incorporated in the following procedures:

(define (cube x) (* x x x))
(define (p x) (- (* 3 x) (* 4 (cube x))))
(define (sine angle)
   (if (not (> (abs angle) 0.1))
       angle
       (p (sine (/ angle 3.0)))))

a.  How many times is the procedure p applied when (sine 12.15) is evaluated?

b.  What is the order of growth in space and number of steps (as a function of a) used by the process generated by the sine procedure when (sine a) is evaluated?

[ 本帖最后由 win_hate 于 2008-9-8 19:31 编辑 ]

论坛徽章:
0
15 [报告]
发表于 2008-09-08 19:36 |只看该作者

Exercise 1.15. 答案

a) 5 次

计算次数的代码为


  1. (ceiling (/ (log 121.5) (log 3)))
复制代码


b) 如果直接用书中代码进行计算,则时间增长和空间增长都是, O(log_3 10a)

不过,因为 sin 是周期函数,所以在调用 sine 之前先约化到 [-pi, pi]  间是合适的。如采用这种方式,则时间和空间都是  O(1) 的。

[ 本帖最后由 win_hate 于 2008-9-8 19:41 编辑 ]

论坛徽章:
0
16 [报告]
发表于 2008-09-09 13:08 |只看该作者

Exercise 1.16.

Exercise 1.16.  Design a procedure that evolves an iterative exponentiation process that uses successive squaring and uses a logarithmic number of steps, as does fast-expt. (Hint: Using the observation that (b^(n/2))^2 = (b^2)^(n/2), keep, along with the exponent n and the base b, an additional state variable a, and define the state transformation in such a way that the product a bn is unchanged from state to state. At the beginning of the process a is taken to be 1, and the answer is given by the value of a at the end of the process. In general, the technique of defining an invariant quantity that remains unchanged from state to state is a powerful way to think about the design of iterative algorithms.)

[ 本帖最后由 win_hate 于 2008-9-9 13:15 编辑 ]

论坛徽章:
0
17 [报告]
发表于 2008-09-09 13:11 |只看该作者

Exercise 1.16. 答案


  1. (define (pow b n)
  2.   (define (f b n c)
  3.     (cond ((= n 0) c)
  4.           ((even? n) (f (* b b) (/ n 2) c))
  5.           (else (f b (- n 1) (* b c)))))

  6.     (f b n 1))
复制代码

论坛徽章:
0
18 [报告]
发表于 2008-09-09 13:16 |只看该作者

Exercise 1.17.

Exercise 1.17.  The exponentiation algorithms in this section are based on performing exponentiation by means of repeated multiplication. In a similar way, one can perform integer multiplication by means of repeated addition. The following multiplication procedure (in which it is assumed that our language can only add, not multiply) is analogous to the expt procedure:

(define (* a b)
  (if (= b 0)
      0
      (+ a (* a (- b 1)))))

This algorithm takes a number of steps that is linear in b. Now suppose we include, together with addition, operations double, which doubles an integer, and halve, which divides an (even) integer by 2. Using these, design a multiplication procedure analogous to fast-expt that uses a logarithmic number of steps.

论坛徽章:
0
19 [报告]
发表于 2008-09-09 13:16 |只看该作者

Exercise 1.17. 答案


  1. (define (mul a b)
  2.   (define (f a b c)
  3.     (cond ((= b 0) c)
  4.           ((even? b) (f (+ a a) (/ b 2) c))
  5.           (else (f a (- b 1) (+ c a)))))

  6.   (f a b 0))
复制代码

论坛徽章:
0
20 [报告]
发表于 2008-09-09 13:20 |只看该作者

Exercise 1.18.

Exercise 1.18.  Using the results of exercises 1.16 and 1.17, devise a procedure that generates an iterative process for multiplying two integers in terms of adding, doubling, and halving and uses a logarithmic number of steps.40
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP