Download presentation
Presentation is loading. Please wait.
Published byJennifer Parker Modified over 9 years ago
1
Tutorial 11 of CSCI2110 Number of Sequence & Recursion Tutor: Zhou Hong ( 周宏 ) hzhou@cse.cuhk.edu.hk
2
HW2 & HW3 has been marked, please pick them up. HW4 will be returned in next Monday. Announcement
3
In your homework, you should declare your cooperators or cite references, this will not affect your grades. – Note: write your OWN solutions, do NOT just copy! Fail to do so may be considered as plagiarism. – The same notation or the same term (e.g. “imaginary women”) are clues. Announcement
4
Outline Number of Sequence Setup Recurrence Relations
5
NUMBER OF SEQUENCE
6
Warm Up Exercise What’s the next number? a 1 = -3, a 2 = -1, a 3 = 1, a 4 = 3, … Answer: a 5 = 5 a 1 = 3, a 2 = 6, a 3 = 12, a 4 = 24, … Answer: a 5 = 48 What about this one? a 1 =1, a 2 =11, a 3 =21, a 4 =1211, a 5 =111221, a 6 =312211, a 7 =13112221, … Answer: a 8 =1113213211 This is just for fun = )
7
Warm Up Exercise (Cont.) Write down the general formula of a i a 1 = -3, a 2 = -1, a 3 = 1, a 4 = 3, … Answer: a i = a 1 + (i-1)d = -3 + 2(i-1) = 2i - 5 a 1 = 3, a 2 = 6, a 3 = 12, a 4 = 24, … Answer: a i = a 1 q (i-1) = 3 x 2 (i-1)
8
Warm Up Exercise (Cont.)
9
Future Value Given bank rate b unchanged, if we deposit $X now, how much will we have after n years? NowAfter 1 YearAfter 2 Years…After n Years $X$X * b$X * b 2 …$X * b n Future Value: 1 dollar today will be worth b n dollars after n years.
10
Current Value Given bank rate b unchanged, if we want to have $X after n years, how much should we deposit now? Now…After n-2 YearsAfter n-1 YearsAfter n Years $X / b n …$X / b 2 $X / b$X Current Value: 1 dollar after n years is only worth 1 / b n dollars today.
11
Mortgage Loan Suppose the bank rate is b and keeps unchanged. Now, we rise a mortgage loan of $X dollars to buy a house. We have to repay the loan in n years. What is the annual repayment $p. (assume repayment is made at the end of each year)
12
Solution 1: Consider Future Value YearAmount to Be Repaid Now$X 1 year$X * b - $p 2 years($X * b – $p) * b – $p = $X * b 2 – $p * b – $p …… n years$X * b n – $p* b n-1 – … – $p * b – $p In order to repay the loan in n years, we must have Therefore, in each year, we have to repay
13
Solution 2: Consider Current Value YearCurrent Value of Each Year’s Payment 1 year$p / b 2 years$p / b 2 …… n years$p / b n The key observation is that the total current value should equal to $X. Therefore, in each year, we have to repay
14
RECURSION Disclaimer: Some of the slides in this section are taken from the slides by Chow Chi Wang, last year’s Tutor of CSCI2110.
15
Recursion Recursion is an important technique in computer science. The key idea is to reduce a problem into the similar problems in simpler cases. In this tutorial, we will focus on how to setup the recurrence relations. We will discuss solving recurrence relations in next week’s tutorial. Tip Tip: After setting up a recurrence relation, remember to test it’s correctness by trying some base cases.
16
Fibonacci Variant
17
Initiation Month 1 Month 2 Month 3 Month 4 Key: Baby rabbit pair Fertile rabbit pair
18
Fibonacci Variant
19
The Josephus Problem Flavius Josephus is a Jewish historian living in the 1st century. During the Jewish-Roman war, he and his 40 soldiers were trapped in a cave, the exit of which was blocked by Romans. The soldiers chose suicide over capture and decided that they would form a circle and proceeding around it, to kill every third remaining person until no one was left. However, Josephus wanted none of this suicide nonsense. His task is to choose a place in the initial circle so that he is the last one remaining and so survive.
20
The Josephus Problem in General Form There are n people numbered 1 to n around a circle. Start with people #1, eliminate every j-th remaining people until only one survives. The task is to find the survivor’s initial number. A simple case with n=10 and j=2: The elimination order: 2, 4, 6, 8, 10, 3, 7, 1, 9. So, people #5 is the survivor.
21
The Case of j=2 Let J(n) be the survivor’s number when start with n people. If n is a even number, we have J(2k) = 2J(k) – 1, for k >= 1. If n is a odd number, we have J(2k+1) = 2J(k) + 1, for k >= 1. Base case, J(1) = 1.
22
The General Value of j Note that, the position of the i-th people in the initial circle is ((i-1) mod n) + 1, starting with people #1. Consider eliminating people one by one. In the very beginning, the j-th people is eliminated, then we start the next count with the j+1-th people starting with people #1. J(n-1) is the survivor’s number in the remaining circle of n-1 people, starting with the j+1-th people in the initial circle. Then, the survivor is the (J(n-1)+j)-th people starting with people #1 in the initial circle. Therefore, we have the following recurrence relation J(1) = 1, J(n) = ((J(n-1)+j-1) mod n) + 1, for n >= 2.
23
Comparison of The Two Recurrence Relations J(1) = 1, J(n) = ((J(n-1)+j-1) mod n) + 1, for n >= 2. J(1) = 1, J(2k) = 2J(k) – 1, for k >= 1, J(2k+1) = 2J(k) + 1, for k >= 1. V. S.
24
Thank You! Q & A ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.