From b695175da3d6abac684a5385f5036046bb86abf8 Mon Sep 17 00:00:00 2001 From: Chinmoy Das Date: Sun, 8 Oct 2017 12:42:13 +0530 Subject: [PATCH 1/6] binary exponentiation --- other/binary_exponentiation.java | 77 ++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 other/binary_exponentiation.java diff --git a/other/binary_exponentiation.java b/other/binary_exponentiation.java new file mode 100644 index 000000000..56461fc81 --- /dev/null +++ b/other/binary_exponentiation.java @@ -0,0 +1,77 @@ +/** +* Binary Exponentiation +* This is a method to find a^b in a time complexity of O(log b) +* This is one of the most commonly used methods of finding powers. +* Also useful in cases where solution to (a^b)%c is required, +* where a,b,c can be numbers over the computers calculation limits. +*/ + +/** + * @author chinmoy159 + * @version 1.0 dated 10/08/2017 + */ +public class bin_expo +{ + /** + * function :- b_expo (int a, int b) + * returns a^b + */ + public static int b_expo(int a, int b) + { + /* + * iterative solution + */ + int res; + for (res = 1; b > 0; a *=a, b >>= 1) { + if ((b&1) == 1) { + res *= a; + } + } + return res; + /* + * recursive solution + if (b == 0) { + return 1; + } + if (b == 1) { + return a; + } + if ((b & 1) == 1) { + return a * b_expo(a*a, b >> 1); + } else { + return b_expo (a*a, b >> 1); + } + */ + } + /** + * function :- b_expo (long a, long b, long c) + * return (a^b)%c + */ + public static long b_expo(long a, long b, long c) + { + /* + * iterative solution + */ + long res; + for (res = 1l; b > 0; a *=a, b >>= 1) { + if ((b&1) == 1) { + res = ((res%c) * (a%c)) % c; + } + } + return res; + /* + * recursive solution + if (b == 0) { + return 1; + } + if (b == 1) { + return a; + } + if ((b & 1) == 1) { + return ((a%c) * (b_expo(a*a, b >> 1)%c))%c; + } else { + return b_expo (a*a, b >> 1)%c; + } + */ + } +} From 677dfe93bb25d5a4faed94ac90388c1bcd82228f Mon Sep 17 00:00:00 2001 From: Chinmoy Das Date: Sun, 8 Oct 2017 12:58:59 +0530 Subject: [PATCH 2/6] Delete binary_exponentiation.java --- other/binary_exponentiation.java | 77 -------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 other/binary_exponentiation.java diff --git a/other/binary_exponentiation.java b/other/binary_exponentiation.java deleted file mode 100644 index 56461fc81..000000000 --- a/other/binary_exponentiation.java +++ /dev/null @@ -1,77 +0,0 @@ -/** -* Binary Exponentiation -* This is a method to find a^b in a time complexity of O(log b) -* This is one of the most commonly used methods of finding powers. -* Also useful in cases where solution to (a^b)%c is required, -* where a,b,c can be numbers over the computers calculation limits. -*/ - -/** - * @author chinmoy159 - * @version 1.0 dated 10/08/2017 - */ -public class bin_expo -{ - /** - * function :- b_expo (int a, int b) - * returns a^b - */ - public static int b_expo(int a, int b) - { - /* - * iterative solution - */ - int res; - for (res = 1; b > 0; a *=a, b >>= 1) { - if ((b&1) == 1) { - res *= a; - } - } - return res; - /* - * recursive solution - if (b == 0) { - return 1; - } - if (b == 1) { - return a; - } - if ((b & 1) == 1) { - return a * b_expo(a*a, b >> 1); - } else { - return b_expo (a*a, b >> 1); - } - */ - } - /** - * function :- b_expo (long a, long b, long c) - * return (a^b)%c - */ - public static long b_expo(long a, long b, long c) - { - /* - * iterative solution - */ - long res; - for (res = 1l; b > 0; a *=a, b >>= 1) { - if ((b&1) == 1) { - res = ((res%c) * (a%c)) % c; - } - } - return res; - /* - * recursive solution - if (b == 0) { - return 1; - } - if (b == 1) { - return a; - } - if ((b & 1) == 1) { - return ((a%c) * (b_expo(a*a, b >> 1)%c))%c; - } else { - return b_expo (a*a, b >> 1)%c; - } - */ - } -} From a36ca7c42f800ff24db287e72ab58f0230b4482c Mon Sep 17 00:00:00 2001 From: Chinmoy Das Date: Sun, 8 Oct 2017 12:59:18 +0530 Subject: [PATCH 3/6] Create binary_exponentiation.py --- other/binary_exponentiation.py | 49 ++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 other/binary_exponentiation.py diff --git a/other/binary_exponentiation.py b/other/binary_exponentiation.py new file mode 100644 index 000000000..57b218b55 --- /dev/null +++ b/other/binary_exponentiation.py @@ -0,0 +1,49 @@ +""" +* Binary Exponentiation +* This is a method to find a^b in a time complexity of O(log b) +* This is one of the most commonly used methods of finding powers. +* Also useful in cases where solution to (a^b)%c is required, +* where a,b,c can be numbers over the computers calculation limits. +* Done using iteration, can also be done using recursion + +* @author chinmoy159 +* @version 1.0 dated 10/08/2017 +""" + + +def b_expo(a, b): + res = 1 + while b > 0: + if b&1: + res *= a + + a *= a + b >>= 1 + + return res + + +def b_expo(a, b, c): + res = 1 + while b > 0: + if b&1: + res = ((res%c) * (a%c)) % c + + a *= a + b >>= 1 + + return res + +""" +* Wondering how this method works ! +* It's pretty simple. +* Let's say you need to calculate a ^ b +* RULE 1 : a ^ b = (a*a) ^ (b/2) ---- example : 4 ^ 4 = (4*4) ^ (4/2) = 16 ^ 2 +* RULE 2 : IF b is ODD, then ---- a ^ b = a * (a ^ (b - 1)) :: where (b - 1) is even. +* Once b is even, repeat the process to get a ^ b +* Repeat the process till b = 1 OR b = 0, because a^1 = a AND a^0 = 1 +* +* As far as the modulo is concerned, +* the fact : (a*b) % c = ((a%c) * (b%c)) % c +* Now apply RULE 1 OR 2 whichever is required. +""" From f4c6578ece43ef1ebcdfcab448c326f72a40763c Mon Sep 17 00:00:00 2001 From: Chinmoy Das Date: Sun, 8 Oct 2017 13:00:13 +0530 Subject: [PATCH 4/6] Binary Exponentiation for a^b --- other/binary_exponentiation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/binary_exponentiation.py b/other/binary_exponentiation.py index 57b218b55..964a646f8 100644 --- a/other/binary_exponentiation.py +++ b/other/binary_exponentiation.py @@ -1,5 +1,5 @@ """ -* Binary Exponentiation +* Binary Exponentiation for Powers * This is a method to find a^b in a time complexity of O(log b) * This is one of the most commonly used methods of finding powers. * Also useful in cases where solution to (a^b)%c is required, From f5917f589c6fe27a642cbf94ef6e230a40ab18f0 Mon Sep 17 00:00:00 2001 From: Chinmoy Das Date: Sun, 8 Oct 2017 13:10:05 +0530 Subject: [PATCH 5/6] Binary Exponentiation for Multiplication --- other/binary_exponentiation_2.py | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 other/binary_exponentiation_2.py diff --git a/other/binary_exponentiation_2.py b/other/binary_exponentiation_2.py new file mode 100644 index 000000000..217a616c9 --- /dev/null +++ b/other/binary_exponentiation_2.py @@ -0,0 +1,50 @@ +""" +* Binary Exponentiation with Multiplication +* This is a method to find a*b in a time complexity of O(log b) +* This is one of the most commonly used methods of finding result of multiplication. +* Also useful in cases where solution to (a*b)%c is required, +* where a,b,c can be numbers over the computers calculation limits. +* Done using iteration, can also be done using recursion + +* @author chinmoy159 +* @version 1.0 dated 10/08/2017 +""" + + +def b_expo(a, b): + res = 0 + while b > 0: + if b&1: + res += a + + a += a + b >>= 1 + + return res + + +def b_expo_mod(a, b, c): + res = 0 + while b > 0: + if b&1: + res = ((res%c) + (a%c)) % c + + a += a + b >>= 1 + + return res + + +""" +* Wondering how this method works ! +* It's pretty simple. +* Let's say you need to calculate a ^ b +* RULE 1 : a * b = (a+a) * (b/2) ---- example : 4 * 4 = (4+4) * (4/2) = 8 * 2 +* RULE 2 : IF b is ODD, then ---- a * b = a + (a * (b - 1)) :: where (b - 1) is even. +* Once b is even, repeat the process to get a * b +* Repeat the process till b = 1 OR b = 0, because a*1 = a AND a*0 = 0 +* +* As far as the modulo is concerned, +* the fact : (a+b) % c = ((a%c) + (b%c)) % c +* Now apply RULE 1 OR 2, whichever is required. +""" From 0393c5ad38d66d19cc7366b4bafd62cd984b049d Mon Sep 17 00:00:00 2001 From: Chinmoy Das Date: Sun, 8 Oct 2017 13:12:33 +0530 Subject: [PATCH 6/6] Update binary_exponentiation.py --- other/binary_exponentiation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/binary_exponentiation.py b/other/binary_exponentiation.py index 964a646f8..1a30fb8fd 100644 --- a/other/binary_exponentiation.py +++ b/other/binary_exponentiation.py @@ -23,7 +23,7 @@ def b_expo(a, b): return res -def b_expo(a, b, c): +def b_expo_mod(a, b, c): res = 1 while b > 0: if b&1: