Calculate Pow(x,n) using recursion
So, there are n Xs and to calculate X to the power n, we simply keep on multiplying with X all the time, exactly n-1 multiplications.
And if we want to write a program to calculate x to the power n, the simplest thing that comes to our mind is that we can start with a variable.
Let’s say we want to start with a variable result(res), initialize it to 1 and then we can simply run a loop from 1 to n and keep multiplying result(res) with x.
So, result becomes result*x.
In this case, you are performing n multiplications because you are initiating the variable to 1 instead of x, but it also takes care of the condition when n is equal to 0 and as we know x to the power 0 or any number to the power 0 is 1.
And as we can see for an input n, we are running just one loop n times, so it should be pretty evident that the time taken here in this case would be proportional to n or we can also say that the time complexity of this algorithm would be O(n).
Now, Albert and Pinto are given an assignment to calculate x to the power n using recursion.
Albert and Pinto come up with two different solutions.
To solve a problem using recursion, we first need to define a recurrence relation or a recursive definition.
Albert says that x to the power n can be written as x*x^(n-1) and this is true for all n greater than 0.
For n=0, x to the power 0 is simply equal to 1.
So, Albert writes a function Pow that takes two arguments x and n, and if n is 0 then simply return 1, else return x*Pow(n-1).
This is Albert’s implementation, and it will work fine for all n>=0.
If we try to calculate x^8 with this algorithm, we make a recursive call, first to calculate x^7, and then x^7 goes on to make a recursive call to calculate x^6, and we keep on making recursive calls till x^0, which simply returns 1.
Now, let’s see what Pinto’s solution is.
Pinto says that x^n can be written as x^(n/2) * x^(n/2) if n is an even number, and x^n is equal to x * x^(n-1) if n is odd. And x^n is equal to 1 if n=0.
So, Pinto also writes a function Pow that takes two arguments x and n.
And it also goes like, if n=0 it return 1, else if n%2==0 then store Pow(x, n/2) in a variable y making recursive call, and return y*y.
Finally, if n is odd then we simply return x * x^(n-1).
This is Pinto’s implementation, and it also works fine for all n>=0.
When we calculate x^8 with this algorithm, then we make a call to x^4, and further x^4 recursively makes a call to calculate x^2 and then x^1, and x^0.
While Albert’s program goes nine steps in this recursion tree, Pinto’s program only goes 5 steps.
If we analyze the recurrence relation of these two algorithms, then Albert’s algorithm’s time taken by is proportional to n, or simply say O(n) in terms of time complexity.
Pinto’s program is O(log n) in terms of time complexity.
↓ 참고 사이트
https://www.youtube.com/watch?v=wAyrtLAeWvI&list=PL2_aWCzGMAwLz3g66WrxFGSXvSsvyfzCO&index=7
'CS > Algorithm' 카테고리의 다른 글
[이코테 2021] 그리디 (Greedy) (0) | 2022.05.21 |
---|---|
[Do it!] 3장. 검색 (Search) (0) | 2022.05.20 |
[Recursion] Fibonacci Sequence - Space Complexity analysis (0) | 2021.09.12 |
[Recursion] Fibonacci Sequence - Recursion with Memoization (0) | 2021.09.12 |
[Recursion] Complexity analysis using factorial (0) | 2021.09.03 |