From 70ac13f4a0fc15f987d3874112dfdeab0af388ac Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Thu, 2 Apr 2020 04:03:22 +0530 Subject: [PATCH 01/12] added binary_exponent.cpp --- math/binary_exponent.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 math/binary_exponent.cpp diff --git a/math/binary_exponent.cpp b/math/binary_exponent.cpp new file mode 100644 index 000000000..0f9d442a5 --- /dev/null +++ b/math/binary_exponent.cpp @@ -0,0 +1,25 @@ +/// C++ Program to find Binary Exponent recursively. + +#include +/* + * Calculating a^b in O(log(b)) by converting b in binary no. + * Binary exponentiation (also known as exponentiation by squaring) + * is a trick which allows to calculate an using only O(logn) multiplications + * (instead of O(n) multiplications required by the naive approach). +*/ + +int binExpo(int a,int b) { + if (b == 0) return 1; + int res = binExpo(a,b/2); + if (b%2) return res*res*a; + else return res*res; +} + +int main() { + int a,b; + /// Give two nos as a^b (where '^' denotes power exponent operation + std::cin >> a >> b; + ///Result of a^b + std::cout << binExpo(a,b) << endl; +} + From c7a50927b74e76e511f70253a4828f57fa9c581c Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Thu, 2 Apr 2020 04:07:01 +0530 Subject: [PATCH 02/12] resolve compile error --- math/binary_exponent.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/binary_exponent.cpp b/math/binary_exponent.cpp index 0f9d442a5..512365c4a 100644 --- a/math/binary_exponent.cpp +++ b/math/binary_exponent.cpp @@ -20,6 +20,6 @@ int main() { /// Give two nos as a^b (where '^' denotes power exponent operation std::cin >> a >> b; ///Result of a^b - std::cout << binExpo(a,b) << endl; + std::cout << binExpo(a,b) << std::endl; } From 55ff267e5ea7bfc5ee7bd5102d6bab2fdccd8b83 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Thu, 2 Apr 2020 04:09:04 +0530 Subject: [PATCH 03/12] Added comments --- math/binary_exponent.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/math/binary_exponent.cpp b/math/binary_exponent.cpp index 512365c4a..c193b79b9 100644 --- a/math/binary_exponent.cpp +++ b/math/binary_exponent.cpp @@ -1,4 +1,4 @@ -/// C++ Program to find Binary Exponent recursively. +///C++ Program to find Binary Exponent recursively. #include /* @@ -8,6 +8,7 @@ * (instead of O(n) multiplications required by the naive approach). */ +///Function to calculate exponent in O(log(n)) using binary exponent. int binExpo(int a,int b) { if (b == 0) return 1; int res = binExpo(a,b/2); From 48287888973106ee513219eb60a1c70a01e42cf1 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Thu, 2 Apr 2020 04:10:26 +0530 Subject: [PATCH 04/12] Resolve type error --- math/binary_exponent.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/binary_exponent.cpp b/math/binary_exponent.cpp index c193b79b9..a8f45e3c5 100644 --- a/math/binary_exponent.cpp +++ b/math/binary_exponent.cpp @@ -18,7 +18,7 @@ int binExpo(int a,int b) { int main() { int a,b; - /// Give two nos as a^b (where '^' denotes power exponent operation + /// Give two nos as a^b (where '^' denotes power exponent operation) std::cin >> a >> b; ///Result of a^b std::cout << binExpo(a,b) << std::endl; From 7b19550f7fb14ba340bf162936f95ab39eca22b6 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Thu, 2 Apr 2020 04:28:03 +0530 Subject: [PATCH 05/12] format binary_exponent.cpp --- math/binary_exponent.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/math/binary_exponent.cpp b/math/binary_exponent.cpp index a8f45e3c5..bd9c1ce38 100644 --- a/math/binary_exponent.cpp +++ b/math/binary_exponent.cpp @@ -1,4 +1,4 @@ -///C++ Program to find Binary Exponent recursively. +/// C++ Program to find Binary Exponent recursively. #include /* @@ -8,7 +8,7 @@ * (instead of O(n) multiplications required by the naive approach). */ -///Function to calculate exponent in O(log(n)) using binary exponent. +/// Function to calculate exponent in O(log(n)) using binary exponent. int binExpo(int a,int b) { if (b == 0) return 1; int res = binExpo(a,b/2); @@ -20,7 +20,7 @@ int main() { int a,b; /// Give two nos as a^b (where '^' denotes power exponent operation) std::cin >> a >> b; - ///Result of a^b + /// Result of a^b std::cout << binExpo(a,b) << std::endl; } From 3bb191c116a72d5ce7b6742dc04545a0078f11ab Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Thu, 2 Apr 2020 04:31:44 +0530 Subject: [PATCH 06/12] resolve format error --- math/binary_exponent.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/math/binary_exponent.cpp b/math/binary_exponent.cpp index bd9c1ce38..7ca388451 100644 --- a/math/binary_exponent.cpp +++ b/math/binary_exponent.cpp @@ -9,18 +9,18 @@ */ /// Function to calculate exponent in O(log(n)) using binary exponent. -int binExpo(int a,int b) { +int binExpo(int a, int b) { if (b == 0) return 1; - int res = binExpo(a,b/2); + int res = binExpo(a, b/2); if (b%2) return res*res*a; else return res*res; } int main() { - int a,b; + int a, b; /// Give two nos as a^b (where '^' denotes power exponent operation) std::cin >> a >> b; /// Result of a^b - std::cout << binExpo(a,b) << std::endl; + std::cout << binExpo(a, b) << std::endl; } From 331fce2203a1074ed39375f4c31eb3cb7211cf3b Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Thu, 2 Apr 2020 04:34:47 +0530 Subject: [PATCH 07/12] resolve else clause formatting error --- math/binary_exponent.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/math/binary_exponent.cpp b/math/binary_exponent.cpp index 7ca388451..8f7a90ca8 100644 --- a/math/binary_exponent.cpp +++ b/math/binary_exponent.cpp @@ -10,10 +10,16 @@ /// Function to calculate exponent in O(log(n)) using binary exponent. int binExpo(int a, int b) { - if (b == 0) return 1; + if (b == 0) { + return 1; + } int res = binExpo(a, b/2); - if (b%2) return res*res*a; - else return res*res; + if (b%2) { + return res*res*a; + } + else { + return res*res; + } } int main() { @@ -23,4 +29,3 @@ int main() { /// Result of a^b std::cout << binExpo(a, b) << std::endl; } - From 085c6943a0c860cd051f2556c6791df4e7cf46ac Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Thu, 2 Apr 2020 04:37:16 +0530 Subject: [PATCH 08/12] resolve else clause braces error --- math/binary_exponent.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/math/binary_exponent.cpp b/math/binary_exponent.cpp index 8f7a90ca8..5c71ca97c 100644 --- a/math/binary_exponent.cpp +++ b/math/binary_exponent.cpp @@ -16,8 +16,7 @@ int binExpo(int a, int b) { int res = binExpo(a, b/2); if (b%2) { return res*res*a; - } - else { + } else { return res*res; } } From bcb3677f7811b9cd78e88974c5a5ddb21e615d27 Mon Sep 17 00:00:00 2001 From: Mann Mehta Date: Sat, 4 Apr 2020 17:28:19 +0530 Subject: [PATCH 09/12] Update math/binary_exponent.cpp Co-Authored-By: John Law --- math/binary_exponent.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/binary_exponent.cpp b/math/binary_exponent.cpp index 5c71ca97c..c9ed944d8 100644 --- a/math/binary_exponent.cpp +++ b/math/binary_exponent.cpp @@ -2,7 +2,7 @@ #include /* - * Calculating a^b in O(log(b)) by converting b in binary no. + * Calculate a^b in O(log(b)) by converting b to a binary number * Binary exponentiation (also known as exponentiation by squaring) * is a trick which allows to calculate an using only O(logn) multiplications * (instead of O(n) multiplications required by the naive approach). From 1d4a67c6ea07b12c5d0b6683dcf402ebce9c914d Mon Sep 17 00:00:00 2001 From: Mann Mehta Date: Sat, 4 Apr 2020 17:29:11 +0530 Subject: [PATCH 10/12] Update math/binary_exponent.cpp Co-Authored-By: John Law --- math/binary_exponent.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/math/binary_exponent.cpp b/math/binary_exponent.cpp index c9ed944d8..8fb9ec1a8 100644 --- a/math/binary_exponent.cpp +++ b/math/binary_exponent.cpp @@ -23,7 +23,7 @@ int binExpo(int a, int b) { int main() { int a, b; - /// Give two nos as a^b (where '^' denotes power exponent operation) + /// Give two numbers a, b std::cin >> a >> b; /// Result of a^b std::cout << binExpo(a, b) << std::endl; From a82e679f2e4de379df42ef24da031c4ea0d1c143 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Sat, 4 Apr 2020 18:11:57 +0530 Subject: [PATCH 11/12] Updated with changes in math/binary_exponent.cpp --- math/binary_exponent.cpp | 46 +++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/math/binary_exponent.cpp b/math/binary_exponent.cpp index 8fb9ec1a8..808a36ce8 100644 --- a/math/binary_exponent.cpp +++ b/math/binary_exponent.cpp @@ -1,14 +1,21 @@ -/// C++ Program to find Binary Exponent recursively. +/// C++ Program to find Binary Exponent Iteratively and Recursively. #include /* - * Calculate a^b in O(log(b)) by converting b to a binary number - * Binary exponentiation (also known as exponentiation by squaring) - * is a trick which allows to calculate an using only O(logn) multiplications - * (instead of O(n) multiplications required by the naive approach). + * Calculate a^b in O(log(b)) by converting b to a binary number. + * Binary exponentiation is also known as exponentiation by squaring. + * NOTE : This is a far better approach compared to naive method which provide O(b) operations. + * Example: + * 10 in base 2 is 1010. + * 2^10 = 2^(1010) = 2^8 * 2^2 + * 2^1 = 2 + * 2^2 = (2^1)^2 = 2^2 = 4 + * 2^4 = (2^2)^2 = 4^2 = 16 + * 2^8 = (2^4)^2 = 16^2 = 256 + * Hence to calculate 2^10 we only need to multiply 2^8 and 2^2 skipping 2^1 and 2^4. */ -/// Function to calculate exponent in O(log(n)) using binary exponent. +/// Recursive function to calculate exponent in O(log(n)) using binary exponent. int binExpo(int a, int b) { if (b == 0) { return 1; @@ -21,10 +28,33 @@ int binExpo(int a, int b) { } } +/// Iterative function to calculate exponent in O(log(n)) using binary exponent. +int binExpo_alt(int a, int b) { + int res = 1; + while (b>0) { + if (b%2) { + res = res*a; + } + a = a*a; + b /= 2; + } + return res; +} + int main() { int a, b; /// Give two numbers a, b std::cin >> a >> b; - /// Result of a^b - std::cout << binExpo(a, b) << std::endl; + if (a==0 && b==0) { + std::cout << "Math error" << std::endl; + } else if (b<0) { + std::cout << "Exponent must be positive !!" << std::endl; + } else { + int resRecurse = binExpo(a, b); + /// int resIterate = binExpo_alt(a, b); + + /// Result of a^b (where '^' denotes exponentiation) + std::cout << resRecurse << std::endl; + /// std::cout << resIterate << std::endl; + } } From 8219e124a7ccd9b01cb2932a0c7f4ec4318e4c46 Mon Sep 17 00:00:00 2001 From: Mann Mehta <44433995+mann2108@users.noreply.github.com> Date: Sat, 4 Apr 2020 18:16:40 +0530 Subject: [PATCH 12/12] Resolve typo errors --- math/binary_exponent.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/math/binary_exponent.cpp b/math/binary_exponent.cpp index 808a36ce8..b4551da25 100644 --- a/math/binary_exponent.cpp +++ b/math/binary_exponent.cpp @@ -31,7 +31,7 @@ int binExpo(int a, int b) { /// Iterative function to calculate exponent in O(log(n)) using binary exponent. int binExpo_alt(int a, int b) { int res = 1; - while (b>0) { + while (b > 0) { if (b%2) { res = res*a; } @@ -45,9 +45,9 @@ int main() { int a, b; /// Give two numbers a, b std::cin >> a >> b; - if (a==0 && b==0) { + if (a == 0 && b == 0) { std::cout << "Math error" << std::endl; - } else if (b<0) { + } else if (b < 0) { std::cout << "Exponent must be positive !!" << std::endl; } else { int resRecurse = binExpo(a, b);