{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04recursion_10

# 04recursion_10 - ENGG1007 Foundations of Computer Science...

This preview shows pages 1–5. Sign up to view the full content.

1 ENGG1007 Foundations of Computer Science Recursion Recursion Prof. Francis Chin, Dr SM Yiu (Chapters 4.3, 7.1)

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

View Full Document
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
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=1 f(n-1)+n n>1 Let n be positive integers

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

View Full Document
4 ENGG1007 FCS Bacteria in a colony The number of bacteria in a colony doubles every hour. If a colony begins with 5 bacteria, how many will be present in n hours? Rabbit on an Island A young pair of rabbits is placed on an island. After they are 2 months old, each pair of rabbits produces another pair each month. What is the number of pairs of rabbits after n months (assuming that no rabbits ever die)? Month Reproducing pairs Young pairs Total pairs 1 0 1 1 2 0 1 1 3 1 1 2 4 1 2 3 5 2 3 5 6 3 5 8 Let a n be # of bacteria in n hours. Then, a 0 = 5; a n+1 = 2a n . Let f n be # of pairs of rabbits after n months. Then, f 1 = 1; f 2 = 1; f n = f n-1 + f n-2 .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 18

04recursion_10 - ENGG1007 Foundations of Computer Science...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online