12. Lecture
12. Lecture
Programming
Lecture No
[email protected] 1
Recursion
What is recursion?
Recursion
See “Recursion”.
Recursion
If you still don't get it, see "Recursion"
2
Recursion
What is recursion?
3
Recursion
Void fun()
{
cout<<“this is fun”<<endl;
fun();
}
4
Recursion
Void fun()
{
cout<<“this is fun”<<endl;
fun();
} When will this end?
5
Recursion
Example of program to print numbers in descending order?
public static int print(int n) {
System.out.println(n); // Print the current value of n
if (n > 1)
{
print(--n); // Recursive call with n-1 (decrement n before passing)
}
else
{
return 1; // Base case: return 1 when n reaches 1
}
}
public static void main(String[] args) {
print(10); // Start the recursion by calling the print function with 10
}
6
Properties of recursive functions
1. Base Case(s): condition for terminating the recursive process
2. Recursive Case(s): set of rules to break the problem into smaller
sub-problems to reach base case
a) Divide the problem into smaller sub-problems
b) Solve the sub-problems
c) Combine results to get answer
7
Need of Base Case and Recursive Case
1 + loop 5
1 + loop 5
infinite loop – no
… termination
8
8
Power function
• Lets figure out a recursive function for calculating
the Powers of a given number
9
Power function
xn = x*xn-1
public static int power (int x, int n)
{
return x * power (x, n-1)
}
10
Power Function
public static int power (int x, int n)
{
return x * power (x, n-1)
Trace table
}
4 2*2*2* 2*power(2,-1) 11
11
Revised Power Function
public static int power (int x, int Base
n) case Trace table: Calc 23: x=2, n=3
{
If (n==0)
return 1; Power (2,3) =8
else 2* power(2,2)
4
2
2* 2* power(2,1)
return x * power (x, n-1)
2*2* 2*power(2,0)
1
}
12
12
Factorial function
Factorial 0! = 1
1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24
13
Factorial function
0! = 1 1*0!
1! = 1 2*1!
2! = 2 * 1 = 2
3*2!
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24 4*3!
……
In general: n!=n*(n-1)!
14
Factorial function
Int factorial (int n)
Trace table: Calc 4! here n=4
{
If (n==0)
factorial (4) =24
return 1;
else 4* factorial
6 (3)
return n * factorial (n-1) 4* 3* factorial
2 (2)
} 4*3* 2* factorial
1 (1)
4*3*2* 1* factorial
1 (0)
15
Factorial function
Version Action Argument or Return Value
1 Call 5
2 Call 4
3 Call 3
4 Call 2
5 Call 1
5 Return 1
4 Return 2
3 Return 6
2 Return 24
1 Return 120
16
Fibonacci sequence
The first ten terms in the sequence are:
1,1,2,3,5,8,13,21,34,55
Each value, except for first two, is sum of last two values
Simply saying:
Fib(n)= fib(n-1)+fib(n-2) except for when n=0 and n=1
17
Function for fibonacci sequence
Int fib (int n)
{
If (n==0 or n==1)
return 1;
else
return fib(n-1) +fib (n-2);
}
18
Trace of Fibonacci(5)
If (n==0 or n==1)
return 1; fib 5 =8
else
return fib(n-1) +fib (n-2);
5 3
fib 4 + fib 3
3 fib 3 + fib 2 2 1
2fib 2 + fib 1
1 fib 1 + fib 0 1
19
Why recursion?
• Recursion makes the • Recursion makes
program faster? the code much
• Recursion uses less simpler and Easy
memory? to read
20