0% found this document useful (0 votes)
15 views

Recursion: L. Mais Saad Safoq Computer Science Department Kerbala University

Recursion is a technique that solves a problem by solving smaller versions of the same problem. A recursive function defines the base case, which provides the known answer, and the general case, which expresses the problem as a smaller version of itself. For a function to be recursive, it must call itself and reduce the problem size until reaching the base case. Examples of recursively defined functions include calculating factorials and dividing numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Recursion: L. Mais Saad Safoq Computer Science Department Kerbala University

Recursion is a technique that solves a problem by solving smaller versions of the same problem. A recursive function defines the base case, which provides the known answer, and the general case, which expresses the problem as a smaller version of itself. For a function to be recursive, it must call itself and reduce the problem size until reaching the base case. Examples of recursively defined functions include calculating factorials and dividing numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Recursion

L. Mais Saad Safoq


Computer Science Department
Kerbala University
May 2020
What is recursion?
 Sometimes, the best way to solve a problem is by solving a
smaller version of the exact same problem first
 Recursion is a technique that solves a problem by solving a
smaller problem of the same type

How do I write a recursive function?


 Determine the base case(s)
(the one for which you know the answer)
 Determine the general case(s)
(the one where the problem is expressed as a smaller version of
itself)
Observations with Respect to a
Recursive Method
1. One of the actions of the method is to invoke (call) itself.
2. Each call passes a smaller version of the same problem, i.e., the
new problem is identical in nature but smaller in size.
3. The method must contain a test for the base case since that case
is handled differently from the others. When you reach the base
case the recursive calls stop.
4. The manner in which the size of the problem diminishes must
ensure that the base case is reached.
Recursion vs. Iteration
 Repetition
 Iteration: explicit loop
 Recursion: repeated function calls
 Termination
 Iteration: loop condition fails
 Recursion: base case recognized
A Recursive Method: The Power of n
 A recursive definition of power(x,n)
power( x, n ) =1 if ( n = 0 )
power( x, n ) = x * power( x, n-1 ) n>0

 function power( int x, int n )


{
if ( n == 0 ) return 1;
else return x * power(x, n-1);
}
power(5,4) 625

power (5,4)
5 * power (5,3) 125

power (5,3)
5 * power (5,2) 25

power(5,2)
5
5 * power (5,1)

1
5 * power(5,0)
Recursively Defined Functions
The Factorial of n
How can we recursively define the factorial function f(n)
= n! ?
factorial(n) = n * [(n-1) * (n-2) * … * 1]
f(0) = 1
f(n) = n f(n-1)
f(0) = 1
f(1) = 1*f(0) = 1*1 = 1
f(2) = 2*f(1) = 2*1 = 2
f(3) = 3*f(2) = 3*2 = 6
f(4) = 4*f(3) = 4*6 = 24
A Recursive Method

 A recurrence relation
 A mathematical formula that generates the terms in a sequence
from previous terms
 Example
factorial(n) = n * [(n-1) * (n-2) * … * 1]
= n * factorial(n-1)
 A recursive definition of factorial(n)
base case factorial(n) = 1 if n = 0

general case n * factorial(n-1) if n > 0


fact (4) 24

fact (4)
6
4 * fact (3) 6

fact (3)
2
3 * fact (2) 2

fact (2)
1
2 * fact (1) 1

fact (1)
The Factorial of n
int factorial(int number) {
int temp;

if(number <= 1) return 1;

temp = number * factorial(number - 1);


return temp;
}
The division of 2 numbers ( a/b)
Division of (a/b) is the count of (a-b) untill a bacame 0

int div(int a, int b) { if(a==0)


if (a<0 ) return 0;
a=0-a; else
if(a<b) {
{int temp=a; a=a-b;
a=b; return 1+div(a,b);
b=temp; }
} }
0

You might also like