Lecture # 03 - New
Lecture # 03 - New
Lecture No 2
Problem
Problem
Effectively, when
power is not divisible
Suppose we want to compute 95
by 2, we make power
even by taking out
the extra 9. Then we
already know the
solution when power
is divisible by 2.
Divide the power by 2
and multiply the base
Department of Computer Science to itself. 20
Example-1:
Brute Force Example
Computing an
Design a recursive algorithm that
computes an
Decrease by a constant
Solution: factor
Recursiv
e
Recursive 1
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 9 7 5
9 7
2*fact(1) 2*1
fact(1): stack = [1,2,3,4]
fact(1) 1
fact(n)
If n<=1 then
return←1 Sequence of Back to
else recursive the calling function
return ←n*fact(n-1) calls
Terminates when
Terminates when the
1 the condition
base case becomes true.
becomes false.
CONCLUSION