-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise1-31.scm
68 lines (54 loc) · 1.31 KB
/
exercise1-31.scm
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
#lang scheme
;section on procedures as arguments
;PART-A
;the sum procedure
(define (product term a next b)
(if (> a b)
1
(* (term a)
(product term (next a) next b))))
;factorial interms of product
;increment function
(define (inc x)
(+ 1 x))
;identity
(define (identity x) x)
;factorial function
(define (factorial n)
(product identity 1 inc n))
;test
(factorial 3)
(factorial 6)
(factorial 9)
;compute pi using
;pi/4 = 2*4*4*6*6*8..../3*3*5*5*7*7
(define (pi-approx n)
(define (piterm x)
(/ (* x (+ x 2.0)) (* (+ x 1.0) (+ x 1.0))))
(define (pinext x)
(+ x 2.0))
(* 4.0 (product piterm 2.0 pinext (* 2.0 n))))
;test
(pi-approx 1)
(pi-approx 100)
(pi-approx 10000)
;PART-B
;iterative form of product
(define (product-iter term a next b)
(define (iter a result)
(if (> a b)
result
(iter (next a) (* (term a) result))))
(iter a 1.0))
;compute pi using
;pi/4 = 2*4*4*6*6*8..../3*3*5*5*7*7
(define (pi-approx-iter n)
(define (piterm x)
(/ (* x (+ x 2.0)) (* (+ x 1.0) (+ x 1.0))))
(define (pinext x)
(+ x 2.0))
(* 4.0 (product-iter piterm 2.0 pinext (* 2.0 n))))
;test
(pi-approx-iter 1)
(pi-approx-iter 100)
(pi-approx-iter 10000)