Presentation is loading. Please wait.

Presentation is loading. Please wait.

Joseph Lindo Recursion Sir Joseph Lindo University of the Cordilleras.

Similar presentations


Presentation on theme: "Joseph Lindo Recursion Sir Joseph Lindo University of the Cordilleras."— Presentation transcript:

1 Joseph Lindo Recursion Sir Joseph Lindo University of the Cordilleras

2 Joseph Lindo Activity Application Definition No Iteration Recursion It is a programming technique in which a call to a method appears in that method’s body Once you understand recursive methods, they are often simpler to write than their iterative equivalents Definition

3 Joseph Lindo No Iteration Definition Activity Application Recursion vs Iteration Problem: Collect $1,000.00 for charity Assumption: Everyone is willing to donate a penny No Iteration

4 Joseph Lindo No Iteration Definition Activity Application Recursion vs Iteration Iterative Solution Visit 100,000 people, asking each for a penny Recursive Solution If you are asked to collect a penny, give a penny to the person who asked you for it No Iteration

5 Joseph Lindo No Iteration Definition Activity Application Recursion Basic Applications Application

6 Joseph Lindo Application No Iteration Definition Activity Recursion Group Activity Create the flow chart of a Fibonacci Sequence with the following conditions: 1 is always the start Accept the number of series to be printed from the user. Activity

7 Joseph Lindo Application No Iteration Definition Activity Recursion Assignment Create the flow chart of a simple Factorial in a one whole yellow paper, wherein the number must come from the user and there is no negative number. Activity

8 Joseph Lindo Recursion --end-- Sir Joseph Lindo University of the Cordilleras

9 Joseph Lindo Definition jo Factorial A factorial is defined as follows: n! = n * (n-1) * (n-2) …. * 1; For example: 1! = 1 (Base Case) 2! = 2 * 1 = 2 3! = 3 * 2 * 1 = 6 4! = 4 * 3 * 2 * 1 = 24 5! = 5 * 4 * 3 * 2 * 1 = 120 If you will study the examples you will start to see a pattern

10 Joseph Lindo Definition jo Factorial Example using the pattern 10 = 10 * 9! 5! = 5 * 4! 4! = 4 * 3!

11 Joseph Lindo Code jo Factorial Iterative approach: int factorial (int n) { int answer = 1; for (int i=1; i<=n; i++) answer *= i; return answer; }

12 Joseph Lindo Code jo Factorial Recursive approach: int factorial (int n) { if (n == 0) return 1; else return n * factorial(n-1); }

13 Joseph Lindo Definition jo Fibonacci Sequence The sequence begins with one Each subsequent number is the sum of the two preceding numbers Fib(n) = Fib(n-1) + Fib(n-2) 1, 1, 2, 3, 5, 8, 13...

14 Joseph Lindo Applications jo Fibonacci Sequence Many aspects of nature are grouped in bunches equaling Fibonacci numbers.

15 Joseph Lindo Applications jo Fibonacci Sequence Music involves several applications of Fibonacci numbers. A full octave is composed of 13 total musical tones, 8 of which make up the actual musical octave.

16 Joseph Lindo Applications jo Fibonacci Sequence Paintings

17 Joseph Lindo Applications jo Fibonacci Sequence Human Body The lengths of bones in a hand are Fibonacci numbers.

18 Joseph Lindo Diagram jo Fibonacci Sequence

19 Joseph Lindo Diagram jo Fibonacci Sequence

20 Joseph Lindo Diagram jo Fibonacci Sequence

21 Joseph Lindo Factorial More Fibonacci Sequence


Download ppt "Joseph Lindo Recursion Sir Joseph Lindo University of the Cordilleras."

Similar presentations


Ads by Google