1 / 19

CS 170 – Intro to Scientific and engineering Programming

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.

bevis
Download Presentation

CS 170 – Intro to Scientific and engineering Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 170 – Intro to Scientific and engineering Programming

  2. 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?

  3. Fibonacci’s rabbits.. • Fibonacci numbers were invented to model the growth of a rabbit colony fib1 = 1 fib2 = 1 fibn = fibn-1 + fibn-2 • 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

  4. CS340 Recursive Thinking

  5. CS340 Recursive Thinking • Recursion reduces a problem into one or more simpler versions of itself

  6. CS340 Recursive Thinking (cont.)

  7. CS340 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.

  8. CS340 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

  9. 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.

  10. CS340 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

  11. CS340 Recursive Thinking (cont.) Recursive Algorithm to Search an Array if the array is empty return -1 as the search result else 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

  12. CS340 Recursive Algorithm for Finding the Length of a String if the string is empty (has no characters) the length is 0 else the length is 1 plus the length of the string that excludes the first character

  13. CS340 Recursive Definitions of Mathematical Formulas

  14. CS340 Recursive Definitions of Mathematical Formulas • Mathematicians often use recursive definitions of formulas • Examples include: • factorials • powers • greatest common divisors (gcd)

  15. CS340 Factorial of n: n! • The factorial of n, or n! is defined as follows: 0! = 1 n! = n x (n -1)! (n > 0) • The base case: n equal to 0 • The second formula is a recursive definition

  16. CS340 Factorial of n: n! (cont.) • The recursive definition can be expressed by the following algorithm: ifn equals 0 n! is 1 else n! = n x (n – 1)! • The last step can be implemented as: return n * factorial(n – 1);

  17. CS340 Infinite Recursion and Stack Overflow • Call factorialwith a negative argument, what will happen? StackOverflowException

  18. Resources • Lecture slides CS112, Ellen Hildreth, http://cs.wellesley.edu/~cs112/

  19. Questions??

More Related