From 7216f28cb318b32eedf420d2fb6274254dd671ff Mon Sep 17 00:00:00 2001 From: DDullahan Date: Mon, 8 Apr 2019 08:57:34 +0800 Subject: [PATCH 1/2] Added FastPower Added FastPower --- src/main/java/com/Others/FastPower.java | 37 +++++++++++++++++++ src/main/java/com/Others/FastPowerTest.java | 40 +++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 src/main/java/com/Others/FastPower.java create mode 100644 src/main/java/com/Others/FastPowerTest.java diff --git a/src/main/java/com/Others/FastPower.java b/src/main/java/com/Others/FastPower.java new file mode 100644 index 00000000..003f1d65 --- /dev/null +++ b/src/main/java/com/Others/FastPower.java @@ -0,0 +1,37 @@ +package src.main.java.com.Others; + +import java.math.BigInteger; + +/** + * We may calculate power with loops, but what if the index is too large ? + * FastPower aims to calculate quickly in this circumstances with time complexity O(log k), + * where k is the index. + * + * @author DDullahan + */ +public class FastPower { + public static BigInteger calculate(BigInteger n, BigInteger k, BigInteger mod) { + BigInteger ans = BigInteger.ONE; + while (!k.equals(BigInteger.ZERO)) { + int odd = k.mod(new BigInteger("2")).compareTo(BigInteger.ZERO); + if(odd == 1){ + ans = ans.multiply(n).mod(mod); + } + k = k.divide(new BigInteger("2")); + n = n.multiply(n).mod(mod); + } + return ans.mod(mod); + } + + public static long calculate(long n, long k, long mod) { + long ans = 1; + while (k != 0) { + if (k % 2 == 1) { + ans = (ans * n) % mod; + } + k >>= 1; + n = (n * n) % mod; + } + return ans % mod; + } +} diff --git a/src/main/java/com/Others/FastPowerTest.java b/src/main/java/com/Others/FastPowerTest.java new file mode 100644 index 00000000..b071c4e4 --- /dev/null +++ b/src/main/java/com/Others/FastPowerTest.java @@ -0,0 +1,40 @@ +package src.main.java.com.Others; + +import org.junit.*; + +import java.math.BigInteger; + +import static org.junit.Assert.*; + +public class FastPowerTest { + + @Test + public void test() { + System.out.println("Long Type:"); + long result; + result = FastPower.calculate(2, 2, 10); + assertEquals(result, 4); + System.out.println("The result of power(2,2) mod 10 is " + result); + + result = FastPower.calculate(100, 1000, 20); + assertEquals(result, 0); + System.out.println("The result of power(100, 1000) mod 20 is " + result); + + result = FastPower.calculate(123456, 123456789, 234); + System.out.println("The result of power(123456, 123456789) mod 234 is " + result); + + + System.out.println("BigInteger Type:"); + BigInteger bigResult; + bigResult = FastPower.calculate(BigInteger.TEN, BigInteger.TEN, new BigInteger("4")); + assertEquals(bigResult, BigInteger.ZERO); + System.out.println("The bigResult of power(10, 10) mod 4 is " + bigResult); + + bigResult = FastPower.calculate(new BigInteger("123456"), new BigInteger("123456789"), new BigInteger("234")); + System.out.println("The bigResult of power(123456, 123456789) mod 234 is " + bigResult); + + bigResult = FastPower.calculate(new BigInteger("123456789101112"), new BigInteger("12345678910111213"), new BigInteger("567890")); + System.out.println("The bigResult of power(123456789101112, 12345678910111213) mod 567890 is " + bigResult); + + } +} \ No newline at end of file From c85caf50d670b4597d8b34576751c18da283f151 Mon Sep 17 00:00:00 2001 From: DDullahan Date: Fri, 3 May 2019 08:56:07 +0800 Subject: [PATCH 2/2] fixed problems of code review from @havanagrawal https://github.com/TheAlgorithms/Java/pull/731/files/7216f28cb318b32eedf420d2fb6274254dd671ff --- src/main/java/com/Others/FastPower.java | 6 ++-- src/main/java/com/Others/FastPowerTest.java | 40 +++++++++------------ 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/Others/FastPower.java b/src/main/java/com/Others/FastPower.java index 003f1d65..8cc1b8b6 100644 --- a/src/main/java/com/Others/FastPower.java +++ b/src/main/java/com/Others/FastPower.java @@ -13,11 +13,11 @@ public class FastPower { public static BigInteger calculate(BigInteger n, BigInteger k, BigInteger mod) { BigInteger ans = BigInteger.ONE; while (!k.equals(BigInteger.ZERO)) { - int odd = k.mod(new BigInteger("2")).compareTo(BigInteger.ZERO); - if(odd == 1){ + int odd = k.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO); + if(odd > 0){ ans = ans.multiply(n).mod(mod); } - k = k.divide(new BigInteger("2")); + k = k.divide(BigInteger.valueOf(2)); n = n.multiply(n).mod(mod); } return ans.mod(mod); diff --git a/src/main/java/com/Others/FastPowerTest.java b/src/main/java/com/Others/FastPowerTest.java index b071c4e4..bd61704b 100644 --- a/src/main/java/com/Others/FastPowerTest.java +++ b/src/main/java/com/Others/FastPowerTest.java @@ -8,33 +8,25 @@ import static org.junit.Assert.*; public class FastPowerTest { + void testLong(long n, long k, long m){ + long result = FastPower.calculate(n,k,m); + assertEquals(result, BigInteger.valueOf(n).modPow(BigInteger.valueOf(k), BigInteger.valueOf(m)).longValue()); + } + + void testBigInteger(BigInteger n, BigInteger k, BigInteger m){ + BigInteger result = FastPower.calculate(n,k,m); + assertEquals(result, n.modPow(k,m)); + } + @Test public void test() { - System.out.println("Long Type:"); - long result; - result = FastPower.calculate(2, 2, 10); - assertEquals(result, 4); - System.out.println("The result of power(2,2) mod 10 is " + result); + testLong(2,2,10); + testLong(100,1000,20); + testLong(123456,123456789,234); - result = FastPower.calculate(100, 1000, 20); - assertEquals(result, 0); - System.out.println("The result of power(100, 1000) mod 20 is " + result); - - result = FastPower.calculate(123456, 123456789, 234); - System.out.println("The result of power(123456, 123456789) mod 234 is " + result); - - - System.out.println("BigInteger Type:"); - BigInteger bigResult; - bigResult = FastPower.calculate(BigInteger.TEN, BigInteger.TEN, new BigInteger("4")); - assertEquals(bigResult, BigInteger.ZERO); - System.out.println("The bigResult of power(10, 10) mod 4 is " + bigResult); - - bigResult = FastPower.calculate(new BigInteger("123456"), new BigInteger("123456789"), new BigInteger("234")); - System.out.println("The bigResult of power(123456, 123456789) mod 234 is " + bigResult); - - bigResult = FastPower.calculate(new BigInteger("123456789101112"), new BigInteger("12345678910111213"), new BigInteger("567890")); - System.out.println("The bigResult of power(123456789101112, 12345678910111213) mod 567890 is " + bigResult); + testBigInteger(BigInteger.TEN,BigInteger.TEN, BigInteger.valueOf(4)); + testBigInteger(new BigInteger("123456"), new BigInteger("123456789"), new BigInteger("234")); + testBigInteger(new BigInteger("123456789101112"), new BigInteger("12345678910111213"), new BigInteger("567890")); } } \ No newline at end of file