diff --git a/Mathematics_Algo/ModularExponentiation b/Mathematics_Algo/ModularExponentiation new file mode 100644 index 0000000..10a2f0c --- /dev/null +++ b/Mathematics_Algo/ModularExponentiation @@ -0,0 +1,21 @@ +//It is a technique to calculate (a^n)%p where p is prime +#include +using namespace std; +int modulararithmetic(int base,int power,int p) +{ + int result=1; + while(power) + { + if(power%2) + { + result=(result*base)%p; + power--; + } + else + { + base=(base*base)%p; + power=power/2; + } + } + return power; +}