From 01ce718ec0a3dc1a6947f172868dfd813274cd42 Mon Sep 17 00:00:00 2001 From: jahnavi chennamsetty Date: Tue, 24 Nov 2020 20:03:20 +0530 Subject: [PATCH 1/3] fast_exponention --- .../bit_manipulation/fast_exponention.py.py | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 algorithms/bit_manipulation/fast_exponention.py.py diff --git a/algorithms/bit_manipulation/fast_exponention.py.py b/algorithms/bit_manipulation/fast_exponention.py.py new file mode 100644 index 00000000..d6d31714 --- /dev/null +++ b/algorithms/bit_manipulation/fast_exponention.py.py @@ -0,0 +1,35 @@ +# Fastest Exponention using Bit Manipulation. + +''' +Convert the integer n into binary form and follow the steps below: + +1.Initialize result with 1 to store a^n. + +2.Traverse until n > 0 and in each iteration, perform Right Shift operation on it. + +3.Also, in each iteration, multiply a with itself and update it. + +4.If current LSB is set, then multiply current value of a to result. + +5.Finally, after completing the above steps, print result. + +''' +def fastExponention(a, n): + result = 1 + while (n > 0): + last_bit = (n & 1) + # Check if current LSB + # is set + if (last_bit): + result = result * a + a = a * a + # Right shift + n = n >> 1 + return result +if __name__ == '__main__': + a = int(input("enter the value of base:")) + n = int(input("enter the value of exponent:")) + print(fastExponention(a,n)) + + + \ No newline at end of file From c3575fbc919382dbcd6fde6989a24363d5b1d0e9 Mon Sep 17 00:00:00 2001 From: Jahnavi Chennamsetty <63448333+jahnavi0105@users.noreply.github.com> Date: Tue, 24 Nov 2020 20:06:34 +0530 Subject: [PATCH 2/3] Rename fast_exponention.py.py to fast_exponention.py --- .../{fast_exponention.py.py => fast_exponention.py} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename algorithms/bit_manipulation/{fast_exponention.py.py => fast_exponention.py} (99%) diff --git a/algorithms/bit_manipulation/fast_exponention.py.py b/algorithms/bit_manipulation/fast_exponention.py similarity index 99% rename from algorithms/bit_manipulation/fast_exponention.py.py rename to algorithms/bit_manipulation/fast_exponention.py index d6d31714..944da276 100644 --- a/algorithms/bit_manipulation/fast_exponention.py.py +++ b/algorithms/bit_manipulation/fast_exponention.py @@ -32,4 +32,4 @@ def fastExponention(a, n): print(fastExponention(a,n)) - \ No newline at end of file + From 20daaf23840a1818561c17b4ef4c1b3b2dc98c02 Mon Sep 17 00:00:00 2001 From: Jahnavi Chennamsetty <63448333+jahnavi0105@users.noreply.github.com> Date: Tue, 24 Nov 2020 20:10:56 +0530 Subject: [PATCH 3/3] fast exponention using bit manipulation --- algorithms/bit_manipulation/fast_exponention.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/algorithms/bit_manipulation/fast_exponention.py b/algorithms/bit_manipulation/fast_exponention.py index 944da276..b2101677 100644 --- a/algorithms/bit_manipulation/fast_exponention.py +++ b/algorithms/bit_manipulation/fast_exponention.py @@ -14,13 +14,13 @@ 5.Finally, after completing the above steps, print result. ''' -def fastExponention(a, n): +def fastExponention(a, n): result = 1 while (n > 0): - last_bit = (n & 1) + last_bit = (n & 1) # Check if current LSB # is set - if (last_bit): + if (last_bit): result = result * a a = a * a # Right shift