From 43ee4cc10ce879a0f5a595e9f25da1eb572caa75 Mon Sep 17 00:00:00 2001 From: lakhan_nad Date: Sun, 25 Oct 2020 15:16:25 +0530 Subject: [PATCH 1/3] Added Binary Exponentiation Fixes:#1943 --- Maths/BinaryPow.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Maths/BinaryPow.java diff --git a/Maths/BinaryPow.java b/Maths/BinaryPow.java new file mode 100644 index 000000000000..5e66d8947a94 --- /dev/null +++ b/Maths/BinaryPow.java @@ -0,0 +1,31 @@ +package Maths; + +public class BinaryPow { + /** + * Calculate a^p using binary exponentiation + * [Binary-Exponentiation](https://cp-algorithms.com/algebra/binary-exp.html) + * + * @param a the base for exponentiation + * @param p the exponent - must be greater than 0 + * @return a^p + */ + public static int BinPow(int a, int p) { + int res = 1; + while (p > 0) { + if ((p & 1) == 1) { + res = res * a; + } + a = a * a; + p >>>= 1; + } + return res; + } + + /** Test Functions */ + public static void main(String[] args) { + int a = 2; + int p = 15; + int res = BinPow(a, p); + System.out.println(a + "^" + p + ": " + res); + } +} From 25300c1c0a163bd247bc8245083419c67d813234 Mon Sep 17 00:00:00 2001 From: lakhan_nad Date: Sun, 15 Nov 2020 23:46:23 +0530 Subject: [PATCH 2/3] tests added in Binary Exponentation #1945 --- Maths/BinaryPow.java | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/Maths/BinaryPow.java b/Maths/BinaryPow.java index 5e66d8947a94..e7f931488cba 100644 --- a/Maths/BinaryPow.java +++ b/Maths/BinaryPow.java @@ -9,7 +9,7 @@ public class BinaryPow { * @param p the exponent - must be greater than 0 * @return a^p */ - public static int BinPow(int a, int p) { + public static int binPow(int a, int p) { int res = 1; while (p > 0) { if ((p & 1) == 1) { @@ -21,11 +21,25 @@ public static int BinPow(int a, int p) { return res; } - /** Test Functions */ - public static void main(String[] args) { - int a = 2; - int p = 15; - int res = BinPow(a, p); + /** + * Function for testing binary exponentiation + * @param a the base + * @param p the exponent + */ + public static void test(int a, int p) { + int res = binPow(a, p); System.out.println(a + "^" + p + ": " + res); } + + /** Main Function to call tests + * + * @param args System Line Arguments + */ + public static void main(String[] args) { + // prints 2^15: 32768 + test(2, 15); + + // prints 3^9: 19683 + test(3,9); + } } From 1f1893abe489c99383240bd4ac252d4215ebb709 Mon Sep 17 00:00:00 2001 From: lakhan_nad Date: Sun, 15 Nov 2020 23:52:33 +0530 Subject: [PATCH 3/3] Assertion Added --- Maths/BinaryPow.java | 1 + 1 file changed, 1 insertion(+) diff --git a/Maths/BinaryPow.java b/Maths/BinaryPow.java index e7f931488cba..d3224c8bcf41 100644 --- a/Maths/BinaryPow.java +++ b/Maths/BinaryPow.java @@ -28,6 +28,7 @@ public static int binPow(int a, int p) { */ public static void test(int a, int p) { int res = binPow(a, p); + assert res == (int) Math.pow(a, p) : "Incorrect Implementation"; System.out.println(a + "^" + p + ": " + res); }