diff --git a/Algebra-and-Numerical-Methods/FastExponentiation.cpp b/Algebra-and-Numerical-Methods/FastExponentiation.cpp new file mode 100644 index 0000000..0f01d39 --- /dev/null +++ b/Algebra-and-Numerical-Methods/FastExponentiation.cpp @@ -0,0 +1,31 @@ +//Calculates a^b +//Time Complexity - log(b), Space Complexity - O(1) + +#include +#define int long long +using namespace std; +int mod = 1e9+7; + +int fastModExponentiation(int a, int b) +{ + int ans=1; + while(b>0) + { + if(b&1){ + ans=(ans*a)%mod; + } + b=b>>1; + a=(a*a)%mod; + } + return ans; +} + +signed main() +{ + cin.tie(NULL); + cout.tie(NULL); + int a, b; + cin>>a>>b; + cout<