feat: add bin pow
This commit is contained in:
@@ -11,6 +11,7 @@
|
|||||||
| EulerNumber | 欧拉数 | [eulers-number](https://hatter.ink/static/resource/muboard/?id=eulers-number&version=latest&full=1) |
|
| EulerNumber | 欧拉数 | [eulers-number](https://hatter.ink/static/resource/muboard/?id=eulers-number&version=latest&full=1) |
|
||||||
| BirthdayParadox | 生日悖论 | [birthday-paradox](https://hatter.ink/static/resource/muboard/?id=birthday-paradox&version=latest&full=1) |
|
| BirthdayParadox | 生日悖论 | [birthday-paradox](https://hatter.ink/static/resource/muboard/?id=birthday-paradox&version=latest&full=1) |
|
||||||
| ChudnovskyAlgorithm | 楚德诺夫斯基算法 | [chudnovsky-algorithm](https://hatter.ink/static/resource/muboard/?id=chudnovsky-algorithm&version=latest&full=1) |
|
| ChudnovskyAlgorithm | 楚德诺夫斯基算法 | [chudnovsky-algorithm](https://hatter.ink/static/resource/muboard/?id=chudnovsky-algorithm&version=latest&full=1) |
|
||||||
|
| BinaryExponentiation | 快速幂 | [binary-exponentiation](https://hatter.ink/static/resource/muboard/?id=binary-exponentiation&version=1) |
|
||||||
|
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
|
|||||||
35
src/main/java/me/hatter/math/BinaryExponentiation.java
Normal file
35
src/main/java/me/hatter/math/BinaryExponentiation.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
package me.hatter.math;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
// https://oi-wiki.org/math/binary-exponentiation/
|
||||||
|
public class BinaryExponentiation {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(binPow(BigInteger.valueOf(2), BigInteger.valueOf(100)));
|
||||||
|
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
for (int j = 0; j < 100; j++) {
|
||||||
|
BigInteger a = BigInteger.valueOf(i).pow(j);
|
||||||
|
BigInteger b = binPow(BigInteger.valueOf(i), BigInteger.valueOf(j));
|
||||||
|
|
||||||
|
if (a.compareTo(b) != 0) {
|
||||||
|
System.out.println(Arrays.asList(i, j, a, b));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BigInteger binPow(BigInteger a, BigInteger b) {
|
||||||
|
BigInteger result = BigInteger.ONE;
|
||||||
|
while (b.compareTo(BigInteger.ZERO) > 0) {
|
||||||
|
if (b.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ONE) == 0) {
|
||||||
|
result = result.multiply(a);
|
||||||
|
}
|
||||||
|
a = a.multiply(a);
|
||||||
|
b = b.divide(BigInteger.valueOf(2));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -46,6 +46,14 @@ public class MathUtil {
|
|||||||
return v.add(BigDecimal.valueOf(nums[0]));
|
return v.add(BigDecimal.valueOf(nums[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static BigInteger max(BigInteger a, BigInteger b) {
|
||||||
|
return (a.compareTo(b) < 0) ? b : a;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BigInteger min(BigInteger a, BigInteger b) {
|
||||||
|
return (a.compareTo(b) < 0) ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
// https://oi-wiki.org/math/number-theory/gcd/
|
// https://oi-wiki.org/math/number-theory/gcd/
|
||||||
public static BigInteger gcd(BigInteger a, BigInteger b) {
|
public static BigInteger gcd(BigInteger a, BigInteger b) {
|
||||||
while (b.compareTo(BigInteger.ZERO) != 0) {
|
while (b.compareTo(BigInteger.ZERO) != 0) {
|
||||||
|
|||||||
@@ -48,6 +48,16 @@ public class MathUtilTest {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMinMax() {
|
||||||
|
Assert.assertEquals(BigInteger.valueOf(10), MathUtil.max(BigInteger.valueOf(1), BigInteger.valueOf(10)));
|
||||||
|
Assert.assertEquals(BigInteger.valueOf(10), MathUtil.max(BigInteger.valueOf(10), BigInteger.valueOf(9)));
|
||||||
|
Assert.assertEquals(BigInteger.valueOf(10), MathUtil.max(BigInteger.valueOf(10), BigInteger.valueOf(10)));
|
||||||
|
Assert.assertEquals(BigInteger.valueOf(10), MathUtil.min(BigInteger.valueOf(10), BigInteger.valueOf(10)));
|
||||||
|
Assert.assertEquals(BigInteger.valueOf(1), MathUtil.min(BigInteger.valueOf(1), BigInteger.valueOf(10)));
|
||||||
|
Assert.assertEquals(BigInteger.valueOf(9), MathUtil.min(BigInteger.valueOf(10), BigInteger.valueOf(9)));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSqrt() {
|
public void testSqrt() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
|
|||||||
Reference in New Issue
Block a user