From 053095808eede1f3677673c30b86fe340c560da5 Mon Sep 17 00:00:00 2001 From: animeshprasad15 <56110479+animeshprasad15@users.noreply.github.com> Date: Sun, 13 Oct 2019 23:09:52 +0530 Subject: [PATCH] added modular exponentiation --- Problem Set 1 Arithmetic/1.6 Find a^b.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Problem Set 1 Arithmetic/1.6 Find a^b.py b/Problem Set 1 Arithmetic/1.6 Find a^b.py index 293ba63..833f290 100644 --- a/Problem Set 1 Arithmetic/1.6 Find a^b.py +++ b/Problem Set 1 Arithmetic/1.6 Find a^b.py @@ -9,9 +9,17 @@ ## Syntax: pow(base,exponent) # exp = pow(a,b) -## Return Modulo for Exponent with Built-in Function -## Syntax: pow(base,exponent, modulo) -# exp = pow(a,b,2) +#function for modular exponentiation i.e., (b^n)%m +## usage pow(b,n,m) +def pow(b,n,m): + r=1 + while n: + if n%1: + r=(r*b)%m + b=(b*b)%m + n>>=1 + return r + ## Using Math Library # import math