diff --git a/algorithms/Maths/fast_power/fast_power_iterative.cpp b/algorithms/Maths/fast_power/fast_power_iterative.cpp new file mode 100644 index 0000000..e18c0d2 --- /dev/null +++ b/algorithms/Maths/fast_power/fast_power_iterative.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +int power(int x, int y) +{ + int res = 1; // Initialize result + + while (y > 0) + { + // If y is odd, multiply x with result + if (y & 1) + res = res*x; + + // y must be even now + y = y>>1; // y = y/2 + x = x*x; // Change x to x^2 + } + return res; +} + + +int main() +{ + int x = 2, y = 10; + + cout<