Skip to content

Commit 06ee625

Browse files
Added Fast Exponent using cpp
1 parent 62706a6 commit 06ee625

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

fastExponent.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
//Fast Exponentitation
5+
int fastExpo(int a, int n)
6+
{
7+
int ans = 1;
8+
9+
while (n > 0)
10+
{
11+
int last_bit = (n & 1);
12+
if (last_bit)
13+
{
14+
ans = ans * a;
15+
}
16+
a = a * a;
17+
n = n >> 1;
18+
}
19+
return ans;
20+
}
21+
22+
int main()
23+
{
24+
25+
int a, n;
26+
cin >> a >> n;
27+
28+
cout << fastExpo(a, n) << endl;
29+
30+
return 0;
31+
}

0 commit comments

Comments
 (0)