Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science - Alice

Similar presentations


Presentation on theme: "Introduction to Computer Science - Alice"— Presentation transcript:

1 Introduction to Computer Science - Alice
Recursion Introduction to Computer Science - Alice

2 Sample code on conditional loops

3 What do these images have in common?

4 They are all RECURSIVE

5 Recursion is common in math
Factorial numbers n! = 1*2*3*...*(n-2)*(n-1)*n Power Series Taylor Series Fibonacci Sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … (keep adding preceding two numbers)

6 Example of Recursion Fibonacci sequence shown as a graph

7 Another example of recursion - Towers of Hanoi
Move the discs from one position to another, where an upper disc must always be smaller than the lower discs

8 Recursion is... In math, an object is defined by itself
In computer science, a function calls itself Need a condition to cause the function to stop calling itself

9 Applications of recursion in computer science
Sorting data Solving tree structure algorithms Drawing fractals Calculating mathematical series, which in turn are used to approximate functions in computer calculations

10 Sample code for Fibonacci
fibonacci(n) ....if n == 0 return 0 ....else if n ==1 return 1 ....else return fibonacci(n-1) + fibonacci(n-2)

11 Recursion vs Conditional-Loops
When do you use one over the other? Conditional loops require less memory and therefore run faster and are easier to read Recursion is better when The condition to end a repetitive behavior is made up of multiple factors, for example a horse race or Towers of Hanoi For mathematical series


Download ppt "Introduction to Computer Science - Alice"

Similar presentations


Ads by Google