2015-10-28 HW14

posted Oct 28, 2015, 4:58 AM by Samuel Konstantinovich   [ updated Oct 29, 2015, 9:30 PM ]
Goal: One more number recursion problem: FibCost

Do Now:
Open Piazza and log in.

Let us use this definition of fib:
(define fib (lambda (n)
              (cond
                ( (= n 0) 0)
                ( (= n 1) 1)
                ( else (+ (fib (- n 1)) (fib (- n 2)))))))

Let us also pretend that each OPERATION costs money.
Operator    Cost
=           $0.01
+           $1.00
-           $1.00

How much does it cost to run each of these functions:
(fib 0)
(fib 1)
(fib 2)
(fib 3)
(fib 4)
(fib 5)
(fib 6)  

How can you calculate the cost of (fib n) ?

Write the (fibcost n) that returns the price of running (fib n). You do NOT use fib in your fibcost function. 



HW14 Due on HW server Friday Oct30th 6am
(1 and 2 were defined in yesterday's notes)
1. (hailLen n) 
2. (maxHail n)
3. (longestHail n) - return the length of the hailstone sequence by looking at the hail of each number from 1 to n. So:

(hailLen 1) 

(hailLen 2) 

... 

(hailLen n)

Which one is longest?


//Test with different values:
(longestHail 1) -> 1
(longestHail 2) -> 2
(longestHail 3) -> 8
(longestHail 4) -> 8
(longestHail 5) -> 8
(longestHail 6) -> 9
(longestHail 7) -> 17
(longestHail 9) -> 20
(longestHail 11) -> 20



Comments