Week 1 Quiz
What does h(3231) return for the following function definition?
def h(x): (m,a) = (1,0) while m <= x: (m,a) = (m*2,a+1) return(a)
Ans:12
What is g(24) - g(23), given the definition of g below?
def g(n): s=0 for i in range(1,n+1): if n%i == 0: s = s+1 return(s)
Ans:6
def f(n): s=0 for i in range(1,n+1): if n%i == 0: s = s+1 return(s%2 == 1)Ans:n is a perfect square.
def f(m): if m == 0: return(0) else: return(m+f(m-1))Ans:The function terminates for nonnegative n with f(n) = n(n+1)/2
No comments