From cab7c1251605e7b6f4846ca0f5016c4205c1fec2 Mon Sep 17 00:00:00 2001 From: Emmanuel Hernandez Date: Tue, 31 Jan 2023 14:52:17 -0600 Subject: [PATCH] write function for fast exponentiation in python --- .../FastExponentiation/main.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Bit-manipulationTechniques/FastExponentiation/main.py diff --git a/Bit-manipulationTechniques/FastExponentiation/main.py b/Bit-manipulationTechniques/FastExponentiation/main.py new file mode 100644 index 0000000..203adf2 --- /dev/null +++ b/Bit-manipulationTechniques/FastExponentiation/main.py @@ -0,0 +1,24 @@ +def fast_exponentiation(a: int, n: int): + ans = 1 + + while n > 0: + last_bit = n & 1 + + if last_bit: + ans *= a + + a *= a + n = n >> 1 + + return ans + + +def main(): + a = int(input()) + n = int(input()) + + print(fast_exponentiation(a, n)) + + +if __name__ == "__main__": + main()