Professional Documents
Culture Documents
12/15/22 SUST-CCSIT 1
Recursive Thinking
Recursion is a programming technique whereby a
method calls itself
هيت قنية ب رمجية حيثت ستدعيا لدا لة ن فسهاRecursion
Infinite recursion occurs when there is no “base
case” which will cause the “calling” to end
Symptom is : running out of memory during
execution
" الالنهائية عندما ال يكون هناك "حالة أساسيةRecursion تحدث ال
12/15/22 2
Recursive Thinking-Triangle Numbers
12/15/22 3
Triangle Numbers
12/15/22 4
12/15/22 5
Base Case
number of pins in 12 rows = 12 + number of pins in 11 rows
number of pins in 11 rows = 11 + number of pins in 10 rows
………
………
number of pins in 3 rows = 3 + number of pins in 2 rows
= 2+1
The number of pins in 1 row is called a base case.
12/15/22 7
Example
Here is our solution for triangle numbers:
Triangle( 1 ) = 1
Triangle( N ) = N + Triangle( N-1 )
Here is another example, calculating Triangle( 5 ):
Triangle( 5 ) = 5 + Triangle( 4 )
= 5 + ( 4 + Triangle( 3 ) )
= 5 + ( 4 + ( 3 + Triangle( 2 ) ))
= 5 + ( 4 + ( 3 + ( 2 + Triangle( 1 ) )))
=5+(4+(3+(2+ 1 )))
= 5 + ( 4 + ( 3 + 3) )
=5+(4+6)
= 5 + 10
= 15
12/15/22 8
Java Method
class TriangleCalc{
int Triangle( int N )
{
if ( N == 1 ) return 1;
else return N + Triangle( N-1 );
}
public static void main ( String[] args) {
int result =Triangle( 4 );
System.out.println("Triangle(4) is " + result ); }}
C:\>java TriangleCalc
Triangle(4) is 10
12/15/22 9
Recursive method
12/15/22 11
Recursive Code
result = 10 public int sum (int N) {
main
int result;
if (N == 1)
result = 6 result =1;
sum else
sum(4) result = N + sum (N-1);
return result;
result = 3 }
sum
sum(3)
result = 1
sum
sum(2)
sum
sum(1)
12/15/22 12
Recursion versus Iteration
12/15/22 13
Example: Factorials
factorial( 0 ) = 1
factorial( N ) = N * factorial( N-1 )
For example,
factorial( 5 ) = 5 * factorial( 4 )
= 5 * ( 4 * factorial( 3 ) )
= 5 * ( 4 * (3 * factorial( 2 ) ) )
= 5 * ( 4 * (3 * (2 * factorial( 1 ) ) ) )
= 5 * ( 4 * (3 * (2 * ( 1 * factorial( 0 ) ) ) ) )
= 5 * ( 4 * (3 * (2 * ( 1 * 1 ) ) ) )
= 5 * 4 * 3 * 2 * 1 * 1 = 120
Often factorial(N) is written N!
12/15/22 14
Different Ways To Compute
Factorials
iterative method (i.e. uses loops):
int factorial( int N )
{
int product = 1;
for ( int j=1; j<=N; j++ )
product *= j;
return product;
}
Different Ways To Compute Factorials
As a simple example, we will talk about a different way to compute
factorials -- using recursion.
This example will not convince you that recursion is useful, that will
come with bigger examples later.
This example will show you how recursion works.
12/15/22 15
int factorial( int N )
{
if ( __________ ) return 1;
else return ______ * _____________;
}
12/15/22 16
A Recursive Method in Java
int factorial(int n) {
int result;
if (n <= 1)
{ // base case
result = 1; // 1! is 1
}
else {
result = n;
int partial = factorial(n-1);
result = result * partial;
} // end else
return result;
} // end factorial
12/15/22 17
Fibonacci
Fibonacci Series
N 1 2 3 4 5 6 7 8 9 10
fib(N) 1 1 2 3 5 8 13 21 34 55
12/15/22 19
Complete Fib
12/15/22 20