0% found this document useful (0 votes)
47 views3 pages

DAA 1.2 Adi

daa worksheet

Uploaded by

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

DAA 1.2 Adi

daa worksheet

Uploaded by

Aditya khanna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING GRADE Discover Learn. Empower Course Name: DAA Lab Course Code: 211TH-311/21CSH-311 Experiment 1.2 Aim: Develop a program for implementation of power function and determine that complexity should be O(log n). Objectives: © Develop a program to efficiently compute the power ofa given base raised to a given exponent using O(log n) complexity. + Implement exponentiation by squaring technique to minimize multiplication operations and achieve faster computation for large exponents. Input/Apparatus Used: Laptop, VS code, C++ Procedure/Algorithm: Input: n (base), power (exponent) Output: result (result of n raised to the power) * The program starts by reading the base a and the exponent b from the user. * It defines a function power(a, b) that calculates the result of raised to the power of b using a recursive divide and conquer approach. * In the power function: © Ifpower is 0, the base case is reached, and the function returns 1. © Itrecursively calculates temp by calling itself with n and power/2 as arguments. Then, it squares temp by multiplying it with itself (emp * temp). If power is even, it directly returns temp If power is odd, it returns temp * n, effectively multiplying an extran term to account for the odd power. ° oo © The power functions called with the provided basen and powerpower, and the result is stored in the variable result. © Finally, the result is printed using the format "n Power power is result", * The program ends. Name: Aditya Khanna vid:2 1 BCS11424 DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING GRAD) Dinowes toon Eopoees SER Ua Course Name: DAA Lab Course Code: 21ITH-311/21CSH-311 * The algorithm follows the same logic as the provided C++ code and achieves logarithmic complexity (O(logn)) by halving the power at each recursion step. Sample Code: #tinclude using namespace std; float power(int a,int b){ if(b>==0){ return 1; 3 float temp=p ower(a,b/2); iffb%2==0){ return temp*temp; } else { if(b>0){ return a*temp*temp; 3 else return (temp *temp)/a; } } int main0){ float a,b; cout<<"Enter Base="; cin>>a Name: Aditya Khanna vid:2 1 BCS11424 DEPARTMENT OF NAAC COMPUTER SCIENCE & ENGINEERING GRADE Diecoes lnom Expres Course Name: DAA Lab Course Code: 211TH-311/21CSH-311 cout<< cin>>b; "Enter exponent="; cout<<"Power is = "<< power(a,b); return 0; Observations/Outcome: PROBLEMS OUTPUT DEBUG CONSOLE aval 9 Time Complexity: The time complexity of given code is O(log n) Learning Outcome: * Learned the concept of dividing a problem into smaller sub-problems and solving them independently. * Learned to analyze the complexity of algorithms and saw that the power function implemented using a divide-and-conquer strategy has a complexity of O(log n). * Gained insights into how mathematical properties can be leveraged to optimize computations. Name: Aditya Khanna vid:2 1 BCS11424

You might also like