CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING
The problem with rabbits… A man puts a pair of rabbits in a place surrounded on all sides by a wall.How many pairs of rabbits can be produced from that pair in a year if it is supposed that every month each pair begets a new pair which from the second month on becomes productive?
Fibonacci’s rabbits..• Fibonacci numbers were invented to model the growth of
a rabbit colonyfib1 = 1fib2 = 1fibn = fibn-1 + fibn-2
• 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
4
Recursive Thinking
Click icon to add picture
CS340
CS340 5
Recursive Thinking• Recursion reduces a problem into one or more simpler
versions of itself
CS340 6
Recursive Thinking (cont.)
CS340 7
Recursive Thinking (cont.)Recursion of Process Nested Dreams
A child couldn't sleep, so her mother told a story about a little frog,
who couldn't sleep, so the frog's mother told a story about a little bear,
who couldn't sleep, so bear's mother told a story about a little weasel
...who fell asleep. ...and the little bear fell asleep; ...and the little frog fell asleep; ...and the child fell asleep.
CS340 8
Steps to Design a Recursive Algorithm Base case:
for a small value of n, it can be solved directly Recursive case(s)
Smaller versions of the same problem Algorithmic steps:
Identify the base case and provide a solution to it Reduce the problem to smaller versions of itself Move towards the base case using smaller versions
Finding… a needle in a haystack• This is a classical computational thinking problem• Write a function: find_needle
• Inputs of your function: the number that you are looking for and an array of numbers
• Outputs of your function: the index of the array where the number was found, OR a message if the number is not in the array.
CS340 10
Recursive Thinking (cont.)• Consider searching for a target value in an array
• With elements sorted in increasing order• Compare the target to the middle element
• If the middle element does not match the target• search either the elements before the middle
element • or the elements after the middle element
• Instead of searching n elements, we search n/2 elements
CS340 11
Recursive Thinking (cont.)Recursive Algorithm to Search an Arrayif the array is empty
return -1 as the search resultelse if the middle element matches the target
return the subscript of the middle element as the result
else if the target is less than the middle element recursively search the array elements before the middle element and return the result
else recursively search the array elements after the middle element and return the result
CS340 12
Recursive Algorithm for Finding the Length of a Stringif the string is empty (has no characters)
the length is 0else
the length is 1 plus the length of the string that excludes the first character
13
Recursive Definitions of Mathematical Formulas
CS340
CS340 14
Recursive Definitions of Mathematical Formulas
• Mathematicians often use recursive definitions of formulas• Examples include:
• factorials• powers• greatest common divisors (gcd)
CS340 15
Factorial of n: n!• The factorial of n, or n! is defined as follows:
0! = 1n! = n x (n -1)! (n > 0)
• The base case: n equal to 0• The second formula is a recursive definition
CS340 16
Factorial of n: n! (cont.) The recursive definition can be expressed by the
following algorithm:if n equals 0
n! is 1else
n! = n x (n – 1)! The last step can be implemented as:
return n * factorial(n – 1);
CS340 17
Infinite Recursion and Stack OverflowCall factorial with a negative argument, what will
happen?
StackOverflowException
Resources• Lecture slides CS112, Ellen Hildreth,
http://cs.wellesley.edu/~cs112/
QUESTIONS??