Question 3-1

Write a Haskell function log2 that takes a parameter n and returns the number of times you can divide n by 2 before reaching 1. For example, log2 25 should return 4, since it takes 4 divisions of 100 until we reach 1:

25 div 2 = 12
12 div 2 = 6
6 div 2 = 3
3 div 2 = 1

Solution

log2 1 = 0 log2 n = 1 + log2 (n `div` 2)

Back to Review for Quiz 3