joseph lindo recursion sir joseph lindo university of the cordilleras
TRANSCRIPT
Joseph Lindo
Recursion
Sir Joseph LindoUniversity of the Cordilleras
Joseph Lindo
Activity
Application
Definition
No Iteration
RecursionRecursionIt 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
DefinitionActivity
Activity
Application
RecursionRecursion
vsIteration
Problem: Collect $1,000.00 for charity
Assumption: Everyone is willing to donate a penny
No Iteration
Joseph Lindo
No Iteration
DefinitionActivity
Activity
Application
RecursionRecursion
vsIteration
Iterative SolutionVisit 100,000 people, asking each for a pennyRecursive SolutionIf you are asked to collect a penny, give a penny to the person who asked you for it
No Iteration
Joseph Lindo
No Iteration
DefinitionActivity
Activity
Application
RecursionBasic
Applications
Application
Joseph Lindo
Application
No Iteration
DefinitionActivity
Activity
RecursionGroup
ActivityCreate the flow chart of a
Fibonacci Sequence with the following conditions:
1 is always the startAccept the number of series
to be printed from the user.Activity
Joseph Lindo
Application
No Iteration
DefinitionActivity
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 LindoUniversity 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 = 23! = 3 * 2 * 1 = 64! = 4 * 3 * 2 * 1 = 245! = 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 BodyThe 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