Skip to content

Commit cc0ddbe

Browse files
committed
Added divide and conquer algorithm
1 parent 31f7ce7 commit cc0ddbe

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Implements algorithm for calculating pow(x, y) using Divide and Conquer technique.
2+
// Time complexity: O(logn)
3+
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
float power(float x, int y)
8+
{
9+
float temp;
10+
if (y == 0)
11+
return 1;
12+
temp = power(x, y / 2);
13+
if (y % 2 == 0)
14+
return temp * temp;
15+
else
16+
{
17+
if (y > 0)
18+
return x * temp * temp;
19+
else
20+
return (temp * temp) / x;
21+
}
22+
}
23+
24+
int main()
25+
{
26+
float x = 23.21;
27+
int y = -5;
28+
cout << power(x, y) << endl;
29+
cout << pow(x, y) << endl;
30+
return 0;
31+
}

0 commit comments

Comments
 (0)