Skip to content
This repository was archived by the owner on Nov 26, 2019. It is now read-only.

Create fast_power_iterative.cpp #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions algorithms/Maths/fast_power/fast_power_iterative.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<bits/stdc++.h>
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<<power(x, y);

return 0;
}