2ENGG1007FCS2Recurrence and recursive functionA function is recursiveif it is defined in terms of itself.Recursive part: by following this recursion, the value of f(n) can be calculated by making progress towards the base case f(4) = f(3) + 4= f(2) + 3 +4= f(1) + 2 + 7= 1 + 9= 10Base case: value of f(n) is knownTwo components of a recursionQ: How to find a “closed form” for f(n)?(how to solve it?)(non-recursive formula)e.g. f(n)= 1n=1f(n-1)+nn>1Let n be positive integers
3ENGG1007FCSf(n) = f(n-1) + n= f(n-2) + (n-1) + n= …..= f(1) + 2 + 3 + … + n= n(n+1)/2One simple approach: Using iteration to guess the answer, then prove it by MIThen, formally prove it using mathematical induction.Base case: n = 1.f(n) = 1; n(n+1)/2 = 1.Induction step: Assume that f(k) = k(k+1)/2 for all k ≥1.Consider n = k+1.f(k+1) = f(k) + k+1= k(k+1)/2 + k + 1 (by hypothesis)= (k2+ 3k + 2)/2= (k+1)(k+2)/2So, f(n) = n(n+1)/2.e.g. f(n)= 1nf(n-1)+nn>1Let n be positive integers=1
This preview has intentionally blurred sections.
Sign up to view the full version.