From 0b518d05a82b43917d76cec6b1fd772d6f04ecc4 Mon Sep 17 00:00:00 2001 From: Rishu Rajan <56962548+RishuRajan@users.noreply.github.com> Date: Thu, 1 Oct 2020 21:24:27 +0530 Subject: [PATCH] Create ModularExponentiation --- Mathematics_Algo/ModularExponentiation | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Mathematics_Algo/ModularExponentiation 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; +}