CS212 Exams
Spring 1999 -
Final

Running Time Analysis


Consider the following definition of a function power which computes xn (i.e., x raised to the nth power):

(define (power x n)
   (cond ((= n 0) 1)
         ((even? n) (power (* x x) (/ n 2)))  ; recursive call #1
         (else (* x (power x (- n 1))))))     ; recursive call #2

If we assume that all primitive operations used in the function (cond, =, *, odd?, -, etc.) require constant time, then determine the running time of power as a function of the magnitude of n. Use big-O notation to express your result.


Solution

Return to CS 212 Final - Spring 1999