1. You are going to write the h4 function, which will calculate the minimum step
ID: 3550993 • Letter: 1
Question
1. You are going to write the h4 function, which will
calculate the minimum steps to finish the tower of Hanoi using 4 pile.
First pick a value of k and move the k smallest disks to one of the temporary pile. Now
we have n - k disks left and we only have 3 piles available (since any of the remaining n - k disks
are larger than the first k disks and cannot be put on top of them). We can solve this by using the
regular algorithm of tower of Hanoi (using 3 piles). We need to look at all possible values of k
and pick the one that produces the smallest number.
The h4 function, in pseudo code, will look something like this:
Function H4 (n):
if n < = 0 then return 0
else
for k from 0 to n - 1
calculate the value of 2 * H4 (k) + H3 (n - k)
create a list containing these n values
return the minimum value of the list.
Hint: you can use list comprehension to create the list and use the minimum function to find the
minimum value. We have discussed list comprehension in class.
Given:
fib :: Int -> Integer
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
memoized_fib :: Int -> Integer
memoized_fib = (map fib [0 ..] !!)
where fib 0 = 0
fib 1 = 1
fib n = memoized_fib (n-2) + memoized_fib (n-1)