You are on page 1of 4

Experiment Title

Student Name: Shreyansh Raut UID: 18bcs1703


Branch: CSE Section/Group- NTPP-2
Semester: 5 Date of Performance: 29/7/2020
Subject Name: DAA LAB Subject Code: CSP-309

Aim/Overview of the practical: Implement power function in O(log n) time complexity .

2. Task to be done/ Which logistics used: To implement the power function with
complexity O(log n).

We divided this problem into sub problems.

X^8=x^4 *x^4

X^4=X^2 *x^2

X^2=x *x

pow(x,n)={x^n/2 *x^n/2, if n is even

x*x^n/2*x^n/2, if n is odd

But in other method whose complexity s O(n)

We directly factorized the value of x

X^8=x*x*x*x*x*x*x*x

3. Algorithm/Flowchart
Step 1: suppose x and n numbers
Step 2: n mod 2

Step 3: result is even then returnpow(x,n/2)*pow(x,n/2)

Step 4: result is odd then return x*pow(x,n/2)*pow(x,n/2)

Step 5: n==0 then return 1

Step 6: END

4. Steps for experiment/practical/Code:


#include <iostream>

using namespace std;

int pow(int x, int n)

if (n == 0)

return 1;

else

return (x * pow(x, n-1));

int main() {

int x,n;
cout<<"enter the vale of x (base) and n (power):"<<endl;

cin>>x>>n;

cout<<x<<" raised to the power "<<n<<" is "<<pow(x, n);

return 0;

5. Observations/Discussions/ Complexity Analysis:

The time complexity is O(logn) algorithm,where x is base and n is exponent value .

6. Result/Output/Writing Summary:

Output:

Learning outcomes (What I have learnt):

1. To find the time complexity


2. To find power of any numbers.
3. To translate algorithm into code format
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):
Sr. No. Parameters Marks Obtained Maximum Marks
1.
2.
3.

You might also like