define times1 function - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

define times1 function

Description:

(define (times-1 function ) (method ((a number ) (n integer )) (if (= n 0) 0 ( a (times-1 a (- n 1) T(0) = c. T(n) = T(n-1) d for n 0. T(0) = O(1) ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 9
Provided by: dexter8
Category:

less

Transcript and Presenter's Notes

Title: define times1 function


1
(define (times-1 ltfunctiongt) (method ((a
ltnumbergt) (n ltintegergt)) (if ( n 0)
0 ( a (times-1 a (- n 1)))))) T(0)
c T(n) T(n-1) d for n gt 0 T(0)
O(1) T(n) T(n-1) O(1) for n gt 0
2
T(0) c T(n) T(n-1) d for n gt 0 T(n)
T(n-1) d T(n-2) d d ...
T(n-n) d d ... d (n d's) T(0)
dn c dn O(n)
3
(define (fast-times ltfunctiongt) (method ((a
ltnumbergt) (n ltintegergt)) (cond (( n 0) 0)
((even? n) (fast-times (double a) (halve
n))) (else ( a (fast-times a (- n
1))))))) T(0) c T(n) T(n/2) c if n is
even, n gt 0 T(n) T(n-1) c if n is odd
4
Easy case b is a power of 2 T(0) c T(n)
T(n/2) c if n is even, n gt 0 T(n) T(n-1)
c if n is odd n 2(log n) T(n) T(n/2)
c T(n/4) c c T(n/8) c c
c ... T(1) (log n) c T(0)
(1 log n) c (2 log n) c
O(log n)
5
n times-1 fast-times 2 0.017
0.0 10 0.017 0.033 100 0.25
0.05 1000 2.45 0.067 10000
24.283 0.067
6
Some Common Patterns (k,c,d are constants) T(0)
c T(n) T(n-k) d T(n) is O(n) T(0)
c T(n) T(n-k) dn T(n) is O(n2) T(0)
c T(n) T(n/k) d T(n) is O(log n) T(0)
c T(n) kT(n/k) d T(n) is O(n) T(0)
c T(n) kT(n/k) dn T(n) is O(n log n)
7
Fibonacci Numbers 1, 1, 2, 3, 5, 8, 13, 21, 34,
55, ... (define (fib1 ltfunctiongt) (method ((n
ltintegergt)) (cond (( n 0) 1) (( n
1) 1) (else ( (fib1 (- n 1)) (fib1 (-
n 2))))))) T(0) O(1) T(1) O(1) T(n) T(n-1)
T(n-2) O(1), n gt 2 T(n) O(Fn)
O(1.62...n)
8
(define (fib2 ltfunctiongt) (method ((n
ltintegergt)) (bind-methods ((fib2-aux
((l ltlistgt) (n ltintegergt)) (if ( n 0)
l (fib2-aux (list (
(first l) (second l))
(first l)) (- n 1)))))
(first (fib2-aux (list 1 1) n))))) T(0)
O(1) T(1) O(1) T(n) T(n-1) O(1), n gt 1 T(n)
O(n)
Write a Comment
User Comments (0)
About PowerShow.com