Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
69 views5 pages

Daa Experiment 2

The document describes an algorithm to calculate x raised to the power of n, or x^n, in O(log n) time complexity using a divide and conquer approach. It breaks down the problem into calculating x raised to the power of n/2, and then multiplies the result accordingly depending on whether n is even or odd. The key steps are: 1) Base case of n = 0 or 1 is handled directly 2) Recursively calculate x raised to the power of n/2 3) Multiply the result either once or twice depending on if n is even or odd This achieves an overall time complexity of O(log n).

Uploaded by

War Lock
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views5 pages

Daa Experiment 2

The document describes an algorithm to calculate x raised to the power of n, or x^n, in O(log n) time complexity using a divide and conquer approach. It breaks down the problem into calculating x raised to the power of n/2, and then multiplies the result accordingly depending on whether n is even or odd. The key steps are: 1) Base case of n = 0 or 1 is handled directly 2) Recursively calculate x raised to the power of n/2 3) Multiply the result either once or twice depending on if n is even or odd This achieves an overall time complexity of O(log n).

Uploaded by

War Lock
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CHANDIGARH UNIVERSITY

UNIVERSITY INSTITUTE OF ENGINEERING


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Submitted By: Akshat Chauhan Submitted To: Lisha yugal

Subject Name Design and Analysis of Algorithms

Subject Code 20CSP-311

Branch CSE

Semester 5
LAB INDEX

Sr.No Program Date Evaluation Sign


LW VV FW Total
(12) (8) (10) (30)

1 Code implements power function 25-08-22


in O (log n) time complexity.
EXPERIMENT 2

NAME: Akshat Chauhan SUBJECT NAME: Design and Analysis of Algorithms


UID: 20BCS5931 SUBJECT CODE: 20CSP-311
SECTION: 20BCS619-A

AIM:

Code implements power function in O (log n) time complexity.

THEORY:
Given two integers x and n, write a function to compute xn. We mayassume that
x and n are small and overflow does not happen.

Algorithm:
Start
Step 1-> declare function to calculate the product of two complex numbers
long long* complex(long long* part1, long long* part2)
Declare long long* ans = new long long[2]
Set ans[0] = (part1[0] * part2[0]) - (part1[1] * part2[1])Se
ans[1] = (part1[1] * part2[0]) + part1[0] * part2[1] return
ans
Step 2-> declare function to return the complex number raised to the power nlong
long* power(long long* x, long long n)
Declare long long* temp = new long long[2]IF
n=0
Set temp[0] = 0Set
temp[1] = 0 return
temp
End
IF n = 1
return xEnd
Declare long long* part = power(x, n / 2)
IF n % 2 = 0
return complex(part, part)
End
return complex(x, complex(part, part))
Step 3 -> In main()
Declare int n
Declare and set long long* x = new long long[2]
Set x[0] = 10
Set x[1] = -11
Set n = 4
Call long long* a = power(x, n)
Stop

Examples:
Input: x = 2, n = 3
Output: 8

Input: x = 7, n = 2
Output: 49
The below solution divides the problem into subproblems of size y/2
and calls the subproblems recursively.

#include <iostream>
using namespace std;
float myPow(float x, int y) {
if(y == 0)
return 1;
float temp = myPow(x, y / 2);
if (y % 2 == 0)
return temp*temp;
else {
if(y > 0)
return x*temp*temp;
else
return (temp*temp)/x;
}
}
int main() {
float x = 5;
int n = 3;
cout<<x<<" raised to the power "<<n<<" is "<<myPow(x, n);
return 0;
}

OUTPUT:

Time Complexity: O(n)


Space Complexity: O(1)
Algorithmic Paradigm: Divide and conquer.

You might also like