Joseph Lindo Recursion Sir Joseph Lindo University of the Cordilleras
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
Joseph Lindo No Iteration Definition Activity Application Recursion vs Iteration Problem: Collect $1, for charity Assumption: Everyone is willing to donate a penny No Iteration
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
Joseph Lindo No Iteration Definition Activity Application Recursion Basic Applications Application
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
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
Joseph Lindo Recursion --end-- Sir Joseph Lindo University of the Cordilleras
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
Joseph Lindo Definition jo Factorial Example using the pattern 10 = 10 * 9! 5! = 5 * 4! 4! = 4 * 3!
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; }
Joseph Lindo Code jo Factorial Recursive approach: int factorial (int n) { if (n == 0) return 1; else return n * factorial(n-1); }
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...
Joseph Lindo Applications jo Fibonacci Sequence Many aspects of nature are grouped in bunches equaling Fibonacci numbers.
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.
Joseph Lindo Applications jo Fibonacci Sequence Paintings
Joseph Lindo Applications jo Fibonacci Sequence Human Body The lengths of bones in a hand are Fibonacci numbers.
Joseph Lindo Diagram jo Fibonacci Sequence
Joseph Lindo Diagram jo Fibonacci Sequence
Joseph Lindo Diagram jo Fibonacci Sequence
Joseph Lindo Factorial More Fibonacci Sequence