brussell 6.001 Tutorial 2, Spring 2004 (lots of material borrowed from benmv) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Upcoming deadlines: Pset 2: Tuesday, 2/17 midnight Lecture 5: Wednesday, 2/18 9am Proj 2: Friday, 2/27 6pm Questions? ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Objective: review the new special form cond, revisit substitution model, practice writing procedures, thinking about orders of growth Details: Procedure-writing: think in terms of problem reduction think in english first ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; cond (with optional else): (cond (p1 e1) (p2 e2) ... (pN eN)) Why is this a special form? How many expressions for each predicate are we allowed to have? Differences between cond and if? Example: (define (foo n) (cond ((= n 1) 1) ((= n 2) 2) ((= n 1) 2))) (foo 1) => 1 (foo 3) => unspecified ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; begin (begin exp1 exp2 ... expN) Why is this a special form? Implicit in cond. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Revisit substitution model (see handout) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Recursive/iterative: delayed ops, syntactical hints iterative if value of call-to-self is directly returned. recursive if value of call-to-self is operated upon before returning. highlight differences between recursive procedure and recursive vs. iterative processes. write sum through repeated successor: (define (sum-r a b) (if (= a 0) b (+ 1 (sum (- a 1) b)))) (define (sum-i a b) (if (= a 0) b (sum (- a 1) (+ b 1)))) ; Problems from recitation handout: ; Write nth order approximation for e = 1+1/fact(1)+1/fact(2)+... (define my-exp (lambda (n) (if (= n 0) 1 (+ (/ 1 (fact n)) (my-exp (- n 1)))))) ; Write nth order approximation to pi/4=1-1/3+1/5-1/7+... (define pi-4 (lambda (n) (cond ((= n 1) 1) ((odd? n) (+ (pi-4 (- n 1)) (/ 1 (- (* 2 n) 1)))) (else (- (pi-4 (- n 1)) (/ 1 (- (* 2 n) 1))))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Orders of growth: in terms of "size of problem" - generally input arguments (sum's order a) think of amount of additional work when add 1 to n do x then do y yields x+y overall do x y times yields x*y overall throw out constants throw out lower order polynomial terms (n^2+n -> n^2) "tight bounds" for problem of size n, generally: new problem = old problem - C --> n new problem = old problem / C --> log n (base C) new problem = (old problem - C) + (old problem - C) --> 2^n ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Human Arithmetic: n is number of digits 6001 +6001 ----- 12002 ; Time (in terms of number of additions) = Theta(n) ; Space (in terms of number of digits to store, not including the ; answer) = Theta(1) 6001 * 6001 ----------- 6001 0000 0000 +36006 ----------- 36012001 ; Time (in terms of number of additions and multiplications) ; = Theta(n^2) ; Space (in terms of number of digits to store, not including the ; answer) = Theta(n) How long in terms of the size of the number (not digits)? ; Time = Theta(log n) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Matrix multiplication A,B are NxN matrices. What is the time complexity of AB? In other words, how many multiplications are there in computing AB? ; Time = Theta(n^3) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Write div: ; implements x/y (integer division) (define (div x y) (if (> y x) 0 (+ 1 (div (- x y) y))))) ; recursive process ; Time? O(x/y) - upper bound by O(x) ; Space? O(x/y) - upper bound by O(x) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Write remainder: ; implement x remainder y (same as remainder) (define (remainder x y) (if (< x y) x (remainder (- x y) y))) ; iterative process ; Time? O(x/y) - upper bound by O(x) ; Space? O(1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Write procedure to check if divisible: ; x divisible by y (define (divisible? x y) (= (mod x y) 0)) ; no looping... ; Time? O(same as mod = x) ; Space? O(1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Write procedure to check if prime: ; Is p prime? (define (prime? p) (define (helper n) (cond ((> n (sqrt p)) #t) ((divisible? p n) #f) (else (helper (+ n 1))))) (helper 2)) ; iterative process ; assuming sqrt takes O(1) time, sqrt(n) * n ; Space? O(1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; fibonacci two ways: (define (fibbad n) (if (< n 2) n (+ (fibbad (- n 1)) (fibbad (- n 2))))) ; recursive process ; Time = Theta((golden ratio)^n) ; Space = Theta(n) (define (fibgood n) (define (helper current prev count) (if (= count 1) current (helper (+ prev current) current (- count 1)))) (helper 1 0 n)) ; iterative process ; Time = Theta(n) ; Space = Theta(1) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Towers of hanoi ; First, define my-display: (define (my-display k from to) (display k) (display " ") (display from) (display " ") (display to) (newline)) ; Now, define hanoi using a helper function: (define (hanoi n) (define (hanoi-helper k from to other) (cond ((= k 1) (my-display k from to)) (else (hanoi-helper (- k 1) from other to) (my-display k from to) (hanoi-helper (- k 1) other to from)))) (hanoi-helper n 1 2 3)) ; Legend says that an order of monks were given the task to complete ; this using 64 golden disks. When their job was done the temple would ; crumble into dust and the world would vanish. How much time would ; this take, say for some arbitrary problem size $n$? How much space? ; ; Run time? O(2^n) Space? O(n) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Potential ideas: Gaussian elimination From PSET: ; Show an iterative procedure that: ; * does not use a helper procedure ; * does not make a call to another procedure with an additional arg. ; Find running time of: (define bar (lambda (n) (cond ((= n 0) 5) ((= n 1) 7) (else (* n (bar (- n 2))))))) ; => Theta(n) (define baz (lambda (n) (cond ((= n 0) 5) ((= n 1) 7) (else (* (baz (- n 1)) (baz (- n 2))))))) ; => Theta(2^n) (define simple-log (lambda (n) (if (= n 1) 0 (+ 1 (simple-log (/ n 2)))))) ; Time => Theta(log n), Space => Theta(log n) ; Rank the following: ; Theta(1) => 1 ; Theta(2^n) => 5 ; Theta(n^2) => 4 ; Theta(2n) => 3 ; Theta(log n) => 2 ; Theta(n) => 3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Write procedure to compute the percentage of primes from 1 to n. (define (prime-percentage n) (define (helper i) (cond ((> i n) 0) ((prime? i) (+ 1 (helper (+ i 1)))) (else (helper (+ i 1))))) (/ (helper 1) n)) ; recursive.. space is O(n/log n) (prime number theorem) ; sum i=0...n i*sqrt(i)...upper bounded by n^2*sqrt(n) ; how bizarre...closed form, anyone? ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Questions? ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; NEXT WEEK: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; let -- syntactic sugar: (let ((var1 exp1) (var2 exp2) ... (varN expN)) body) same as: ((lambda (var1 var2 ... varN) body) exp1 exp2 ... expN) Why is this a special form? How many expressions are we allowed per each variable assignment? Example 1: (let ((x 3) (y 4)) (+ x y)) ((lambda (x y) (+ x y)) 3 4) => 7 Example 2: (define x 2) (let ((x 3) (y (+ x 2))) (* x y)) ((lambda (x y) (* x y)) 3 (+ x 2)) => 12