-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection11.rkt
127 lines (89 loc) · 2.01 KB
/
section11.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#lang sicp
486
(+ 40 86)
(* 45 89)
(define size 2)
size
(define (power base exp)
(cond ((= exp 0) 1)
(else
(* base (power base (- exp 1))))))
(power 2 5)
(power 3 3)
(define pi 3.14159)
(define radius 10)
(* pi (* radius radius))
(define circumference
(* 2 pi radius))
circumference
(define area
(* pi (expt radius 2)))
area
(define (square x)
(* x x))
(define (sum-squares x y)
(+ (square x) (square y)))
(sum-squares 20 20)
(define (even x)
(= (modulo x 2) 0))
(define (even-only x)
(cond ((even x) "hooray it is even!")
(else "aww it is false.")))
(even-only 2)
(even-only 3)
; EX 1.2
(/ (+ 5 4 (- 2
(- 3
(+ 6 (/ 4 5)))))
(* 3 (- 6 2) (- 2 7)))
; EX 1.3
(define (sum-squares-largest a b c)
(cond ((and (<= a b) (<= a c)) (sum-squares b c))
((and (<= b a) (<= b c)) (sum-squares a c))
(else (sum-squares a b))))
(sum-squares-largest 1 2 3)
(sum-squares-largest 4 4 2)
(sum-squares-largest 3 2 1)
(sum-squares-largest 1 1 1)
(sum-squares-largest 3 2 1)
; EX 1.5
(define (p)
(p))
(define (test x y)
(if (= x 0) 0 y))
; EX 1.7
(define (abs x)
(if (< x 0) (- x) x))
; test if x is approx a good root for y with bound
(define (root-test-approx guess x bound)
(<= (abs (- x (* guess guess))) (abs bound)))
(define (improve guess x)
(/ (+ guess (/ x guess)) 2))
(define (newtons-method guess x)
(if (root-test-approx guess x .00001)
guess
(newtons-method (improve guess x) x)))
(define (sqrt x)
(newtons-method 1 x))
(sqrt 4)
(sqrt 3)
(sqrt 2)
; EX 1.8
; new guess test
(define (root-test-approx2 old-guess guess bound)
(<= (/ (abs (- guess old-guess))
guess)
bound))
(define (improve2 guess x)
(/ (+ guess (/ x guess)) 2))
(define (newtons-method2 old-guess guess x)
(if (root-test-approx2 old-guess guess .00001)
guess
(newtons-method2 guess (improve2 guess x) x)))
(define (sqrt2 x)
(newtons-method2 0 1 x))
(sqrt2 4)
(sqrt2 3)
(sqrt2 2)
(sqrt2 .0006)
(sqrt2 100000)