天天看点

sicp 第一章 习题

1.1

10

12

8

3

6

19

#f

4

16

6

16

1.2

(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 1 3)))))(* 3 (- 6 2)(- 2 7)))

1.3

(define (sum x y z)

  (+ (square(larger x y))(square (larger (smaller x y) z))))

(define (square x)

  (* x x))

(define (larger x y)

  (if(< x y)

     y

     x

     ))

(define (smaller x y)

  (if (< x y)

      x

      y))

1.4

if b>0,a+b

if b<=0,a-b

1.5

applicative order

1.6

running forever

在applicative order下,编译器会计算所有的参数值,递归函数会被无限次计算

1.7

(define (good-enough2? guess1 guess2)

  (< (/(abs (- guess1 guess2)) guess1) 0.0001))

it works pretty well for small and large numbers.

1.8

(define (cuberoot guess x)

  (if (good-enough? guess (improve guess x))

      guess

      (cuberoot (improve guess x) x)))

(define (good-enough? guess1 guess2)

  (< (/(abs (- guess1 guess2)) guess1) 0.0001))

(define (improve guess x)

  (/ (+ (* 2 guess) (/ x (* guess guess))) 3))

(define (cube-root x)

  (cuberoot 1.0 x))

1.9

(define (+ a b)

  (if (= a 0)

      b

      (inc (+ (dec a) b))))

|(+ 4 5)

| (+ 3 5)

| |(+ 2 5)

| | (+ 1 5)

| | |(+ 0 5)

| | |5

| | 6

| |7

| 8

|9

递归

(define (+ a b)

  (if (= a 0)

      b

      (+ (dec a) (inc b))))

|(p 4 5)

|(p 3 6)

|(p 2 7)

|(p 1 8)

|(p 0 9)

|9

迭代

1.10

> (A 1 10)

1024

> (A 2 4)

65536

> (A 3 3)

65536

(f n)=2*n

(g n)=2^n

(h n)=2^(2^(2^(2^(...))))n个2

1.11

recursive

(define (f n)

  (cond ((< n 3) n)

        (else (+(f (- n 1))

                (* 2 (f (- n 2)))

                (* 3 (f (- n 3)))))))

iterative

(define (f1 n)

  (if (< n 3)

      n

      (ff 0 1 2 n)))

(define (ff a b c n)

  (if (= n 2)

      c

      (ff b c (+ c (* 2 b)(* 3 a)) (- n 1))))

1.12

(define (pascal x y)

  (if (or (= y 1)(= x 1)(= x 2)(= x y))

      1

      (+ (pascal (- x 1) y) (pascal (- x 1) (- y 1)))))

1.14

                    11

                  10、5、1

                   /     /

                  11      1

                  5、1   5、1

                 /  /       /

                11   6       1

                 1  5、1     1

                   /   /

                   6    1

                   1    1

空间复杂度:O(n) 时间复杂度:O(n^5)

1.15

5次

空间复杂度:O(a)时间复杂度:O(log a)

1.16

(define (expt b n)

  (expt-iter 1 b n))

(define (expt-iter a b n)

  (cond ((= n 0) a)

        ((even? n) (expt-iter a (* b b) (/ n 2)))

        (else (expt-iter (* a b) b (- n 1)))))

1.17

(define (* a b)

  (cond ((= b 0) 0)

        ((even? b) (double (* a(halve b))))

        (else (+ a (* a (- b 1))))))

1.18

(define (* a b)

  (*-iter 0 a b))

(define (*-iter s a b)

  (cond ((= b 0) s)

        ((even? b) (*-iter s (double a) (halve b)))

        (else (*-iter (+ s a) a (- b 1)))))

1.19

(define (fib n)

  (fib-iter 1 0 0 1 n))

(define (fib-iter a b p q count)

  (cond ((= count 0) b)

        ((even? count) (fib-iter a

                                 b

                                 (+(* p p) (* q q))

                                 (+ (* q q) (* 2 p q))

                                 (/ count 2)))

        (else (fib-iter (+ (* b q) (* a q) (* a p))

                        (+ (* b p) (* a q))

                        p

                        q

                        (- count 1)))))

1.20

applicative order 4次

normal order

1.21

199 199

1999 1999

19999 7

1.22

网上说 plt scheme没有 runtime,用 current-inexact-millisecond。

(define (t n)

  (newline)

  (display n)

  (start-prime-test n (current-inexact-milliseconds)))

(define (start-prime-test n start-time)

  (if (prime? n)

      (report-prime (- (current-inexact-milliseconds) start-time))))

(define (report-prime elapsed-time)

  (display " *** ")

  (display elapsed-time))

(define (search-for-primes n c)

  (cond ((= c 0) (display "over"))

        ((prime? n)(display n)(newline)(search-for-primes (+ n 1) (- c 1)))

        (else (search-for-primes (+ n 1) c))))

> (search-for-primes 1000000000 3)

1000000007 31

1000000009 31

1000000021 15

平均时间1:25.667

> (search-for-primes 10000000000 3)

10000000019 78

10000000033 93

10000000061 94

平均时间2:88.333

> (search-for-primes 100000000000 3)

100000000003 312

100000000019 290

100000000057 312

平均时间3:304.667

平均时间2/平均时间1=3.442

平均时间3/平均时间2=3.449

1.23

(define (smallest-divisor n)

  (find-divisor n 2))

(define (find-divisor n test-divisor)

  (cond ((> (square test-divisor) n) n)

        ((divides? test-divisor n) test-divisor)

        (else (find-divisor n (next test-divisor)))))

(define (next test)

  (if (= test 2)

      3

      (+ test 2)))

1000000007 16.0

1000000009 16.0

1000000021 15.0

平均时间1:15.667

25.667/15.667=1.64

10000000019 47.0

10000000033 47.0

10000000061 47.0

平均时间2:47

88.333/47=1.88

100000000003 187.0

100000000019 171.0

100000000057 172.0

平均时间2:176.667

304.667/176.667=1.72

差不多是以前的一半

1.24

random<2^32 太小了,搞不出来

1.25

b^e可能会非常大,非常大

1.26

每个过程都调用,复杂度变为O(2^(log n))=O(n)

1.27

(define (t n)

  (test 2 n))

(define (test a n)

  (cond ((= a n)(display "OK"))

        ((= (expmod a n n) a) (test (+ a 1) n))

        (else (display "fail"))))

1.28

看不懂题目

1.29

(define (sum f a b n c)

  (if (= c n)

      (f b)

      (cond((= c 0)(+(f a)

                     (sum f a b n (+ c 1))))

           ((even? c)(+(* 2(f (+ a (* c (h a b n)))))

                       (sum f a b n (+ c 1))))

           (else (+(* 4(f (+ a (* c (h a b n)))))

                   (sum f a b n (+ c 1)))))))

(define (cube x)

  (* x x x))

(define (s f a b n)

  (* (/(h a b n)3)(sum f a b n 0)))

(define (h a b n)

  (/ (- b a) n))

> (integral cube 0 1 0.01)

0.24998750000000042

> (integral cube 0 1 0.001)

0.249999875000001

> (s cube 0 1 100)

1/4

> (s cube 0 1 1000)

1/4

1.30

(define (sum term a next b)

  (define (iter a result)

    (if (> a b)

        result

        (iter (next a) (+ result ( term a)))))

  (iter a 0))

1.31

(a)定义乘法高阶函数

(define (product f next a b)

  (if (> a b)

      1.0

      (* (f a) (product f next (next a) b))))

计算PI、递归过程:

(define (inc a)

  (+ a 2))

(define (pi b)

  (product div inc 2 b))

(define (div x)

  (/ (* x (+ x 2))(* (+ x 1)(+ x 1))))

(b)迭代过程

(define (product f next a b result)

  (if (> a b)

      result

      (product f next (next a) b (* result (f a)))))

1.32

递归过程

(define (accumulate combiner null-value term a next b)

  (if (> a b)

      null-value

      (combiner (term a)

                (accumulate combiner null-value term (next a) next b))))

迭代过程

(define (accumulate combiner term a next b result)

  (if (> a b)

      result

      (accumulate combiner

                  term

                  (next a)

                  next

                  b

                  (combiner result (term a)))))

1.33

递归的更高阶的filtered-accumulate

(define (filtered-accumulate combiner

                             term

                             filter

                             null-value

                             a

                             next

                             b)

  (cond ((> a b) null-value)

        ((filter a) (combiner (term a)

                              (filtered-accumulate combiner

                                                   term

                                                   filter

                                                   null-value

                                                   (next a)

                                                   next

                                                   b)))

        (else (combiner null-value (filtered-accumulate combiner

                                                      term

                                                      filter

                                                      null-value

                                                      (next a)

                                                      next

                                                      b)))))

(a)

(define (prime-sum a b)

  (filtered-accumulate +

                       square

                       prime?

                       a

                       inc

                       b))

(b)

(define (gcd a b)

  (if (= (remainder a b) 0)

      b

      (gcd b (remainder a b))))

(define (integer x)

  x)

(define (relative-prime-product n)

  (define (relative-prime? a)

    (if (=(gcd n a) 1)

    #t

    #f))

  (filtered-accumulate *

                       integer

                       relative-prime?

                       1

                       1

                       inc

                       n))

1.34

(f f)->(f 2)->(2 2) stop

1.35

> (fixed-point (lambda (x) (+ 1 (/ 1 x))) 1.0)

1.6180327868852458

1.36

without average damping:

> (fixed-point (lambda (y) (/ (log 1000) (log y))) 10)

10

2.9999999999999996

6.2877098228681545

3.7570797902002955

5.218748919675316

4.1807977460633134

4.828902657081293

4.386936895811029

4.671722808746095

4.481109436117821

4.605567315585735

4.522955348093164

4.577201597629606

4.541325786357399

4.564940905198754

4.549347961475409

4.5596228442307565

4.552843114094703

4.55731263660315

4.554364381825887

4.556308401465587

4.555026226620339

4.55587174038325

一共22步

with average damping:

> (fixed-point (lambda (y) (average y (/ (log 1000) (log y)))) 10)

10

6.5

5.095215099176933

4.668760681281611

4.57585730576714

4.559030116711325

4.55613168520593

4.555637206157649

一共7步

简单变换一下方程,效率提高好多

1.37

> (cout-frac 1 1.0 11)

0.6180555555555556

迭代过程

(define (cout-frac n d k result)

  (if (= k 0)

      (* 1.0 result)

      (cout-frac n d (- k 1) (/ (n k) (+ (d k) result)))))

1.38

(define (cout-frac n d k result)

  (if (= k 0)

      (* 1.0 result)

      (cout-frac n d (- k 1) (/ (n k) (+ (d k) result)))))

(define (Dk k)

  (let ((re (remainder k 3)))

  (cond ((= re 0) 1)

        ((= re 1) 1)

        (else (*(/(+ k 1)3)2)))))

(define (Nk k)

  1)

(define (e k)

  (+ 2 (cout-frac Nk Dk k 0)))

1.39

(define (tan-cf x k)

  (define (Nk k)

    (if (= k 1)

        x

        (* x x)))

  (define (Dk k)

    (- (* 2 k) 1))

  (define (cout-frac n d k result)

    (if (= k 0)

        (* 1.0 result)

        (cout-frac n d (- k 1) (/ (n k) (- (d k) result)))))

  (cout-frac Nk Dk k 0))

1.40

(define (cubic a b c)

  (lambda (x) (+(* x x x)(* a x x)(* b x)c)))

1.41

(define (double g)

  (lambda(x) (g (g x))))

> (((double (double double)) inc ) 5)

21

1.42

(define (compose f g)

  (lambda (x) (f (g x))))

1.43

(define (repeated f x)

  (if (= x 1)

      (lambda (y) (f y))

      (compose f (repeated f (- x 1)))))

1.44

(define (smooth f)

  (lambda (x) (/ (+(f (+ x dx)) (f x) (f(- x dx)))3)))

(define (n-fold-smooth f n)

  ((repeated smooth n)f))

1.45

(define (n-root n x) 

  (define (times i)   

    (if (< i 2)

        (+ 1 (times (/ i 2))))) 

  (define (div y)   

    (/ x (fast-expt y (- n 1)))) 

  (fixed-point ((repeated average-damp (times n)) div) 1.0))

1.46

(define (iterative-improve f g)

  (define (try x)

    (if(f x)

       x

       (try (g x))))

  (lambda (x) (try x)))