Skip to content

Commit

Permalink
Added Modular Exponentiation Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Sahilkalamkar committed Oct 13, 2019
1 parent 7cabbf5 commit 09c03a8
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions modularExponentiation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int mod = 1e9+7;

ll modularExponentiation(ll base,ll exponent,ll modulo)
{
if(exponent==0)
return 1;


if(!(exponent&1))
{
ll res = modularExponentiation(base,exponent/2,modulo);
return (res*res)%modulo;
}
else
return ((base%modulo)*modularExponentiation(base,exponent-1,modulo))%modulo;
}

int main()
{
int base,power,m;
cin>>base>>power>>m;

cout<<modularExponentiation(base,power,m)<<'\n';
return 0;
}

0 comments on commit 09c03a8

Please sign in to comment.