{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04recursion_10

04recursion_10 - ENGG1007 Foundations of Computer Science...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ENGG1007 Foundations of Computer Science Recursion Recursion Prof. Francis Chin, Dr SM Yiu (Chapters 4.3, 7.1)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 ENGG1007 FCS 2 Recurrence and recursive function A function is recursive if 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 = 10 Base case: value of f(n) is known Two components of a recursion Q: How to find a “ closed form ” for f(n)? (how to solve it?) ( non-recursive formula ) e.g. f(n)= 1 n=1 f(n-1)+n n>1 Let n be positive integers
Image of page 2
3 ENGG1007 FCS f(n) = f(n-1) + n = f(n-2) + (n-1) + n = ….. = f(1) + 2 + 3 + … + n = n(n+1)/2 One simple approach: Using iteration to guess the answer, then prove it by MI Then, 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) = (k 2 + 3k + 2)/2 = (k+1)(k+2)/2 So, f(n) = n(n+1)/2. e.g. f(n)= 1 n f(n-1)+n n>1 Let n be positive integers =1
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Do