feat: add binomial theorem
This commit is contained in:
30
README.md
30
README.md
@@ -1,19 +1,21 @@
|
||||
# math
|
||||
|
||||
| Class | Comments | MuBoard Link |
|
||||
|-------------------------|--------------------|-----------------------------------------------------------------------------------------------------------------------------|
|
||||
| BinaryExponentiation | 快速幂 | [binary-exponentiation](https://hatter.ink/static/resource/muboard/?id=binary-exponentiation&version=latest&full=1) |
|
||||
| BirthdayParadox | 生日悖论 | [birthday-paradox](https://hatter.ink/static/resource/muboard/?id=birthday-paradox&version=latest&full=1) |
|
||||
| ChineseRemainderTheorem | 中国剩余定理 | [chinese-remainder-theorem](https://hatter.ink/static/resource/muboard/?id=chinese-remainder-theorem&version=latest&full=1) |
|
||||
| ChudnovskyAlgorithm | 楚德诺夫斯基算法 | [chudnovsky-algorithm](https://hatter.ink/static/resource/muboard/?id=chudnovsky-algorithm&version=latest&full=1) |
|
||||
| EulerNumber | 欧拉数 | [eulers-number](https://hatter.ink/static/resource/muboard/?id=eulers-number&version=latest&full=1) |
|
||||
| EulerTheorem | 欧拉定理 | [euler-theorem](https://hatter.ink/static/resource/muboard/?id=euler-theorem&version=latest&full=1) |
|
||||
| FibonacciSequence | 斐波那契数列 | [fibonacci-sequence](https://hatter.ink/static/resource/muboard/?id=fibonacci-sequence&version=latest&full=1) |
|
||||
| LeibnizFormula | $\pi$ 的莱布尼茨公式 | [leibniz-formula](https://hatter.ink/static/resource/muboard/?id=leibniz-formula&version=1) |
|
||||
| ModularInv | 求乘法逆元【扩展欧几里得法】 | - |
|
||||
| Prime | 试除法 | - |
|
||||
| Sqrt10 | 求 $\sqrt{10}$【连分数】 | [resolve-sqrt-10](https://hatter.ink/static/resource/muboard/?id=resolve-sqrt-10&version=latest&full=1) |
|
||||
| WallisProduct | 沃里斯乘积 | [wallis-product](https://hatter.ink/static/resource/muboard/?id=wallis-product&version=latest&full=1) |
|
||||
| Class | Comments | MuBoard Link |
|
||||
|-------------------------|--------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||
| BinaryExponentiation | 快速幂 | [binary-exponentiation](https://hatter.ink/static/resource/muboard/?id=binary-exponentiation&version=latest&full=1) |
|
||||
| BinomialTheorem | 二项式定理 | [binomial-theorem](https://hatter.ink/static/resource/muboard/?id=binomial-theorem&version=latest&full=1) |
|
||||
| BirthdayParadox | 生日悖论 | [birthday-paradox](https://hatter.ink/static/resource/muboard/?id=birthday-paradox&version=latest&full=1) |
|
||||
| ChineseRemainderTheorem | 中国剩余定理 | [chinese-remainder-theorem](https://hatter.ink/static/resource/muboard/?id=chinese-remainder-theorem&version=latest&full=1) |
|
||||
| ChudnovskyAlgorithm | 楚德诺夫斯基算法 | [chudnovsky-algorithm](https://hatter.ink/static/resource/muboard/?id=chudnovsky-algorithm&version=latest&full=1) |
|
||||
| EulerNumber | 欧拉数 | [eulers-number](https://hatter.ink/static/resource/muboard/?id=eulers-number&version=latest&full=1) |
|
||||
| EulerTheorem | 欧拉定理 | [euler-theorem](https://hatter.ink/static/resource/muboard/?id=euler-theorem&version=latest&full=1) |
|
||||
| FibonacciSequence | 斐波那契数列 | [fibonacci-sequence](https://hatter.ink/static/resource/muboard/?id=fibonacci-sequence&version=latest&full=1) |
|
||||
| LeibnizFormula | $\pi$ 的莱布尼茨公式 | [leibniz-formula](https://hatter.ink/static/resource/muboard/?id=leibniz-formula&version=1) |
|
||||
| ModularInv | 求乘法逆元【扩展欧几里得法】 | - |
|
||||
| NormalDistribution | 正态分布 | [normal-distribution](https://hatter.ink/static/resource/muboard/?id=normal-distribution&version=latest&full=1) |
|
||||
| Prime | 试除法 | - |
|
||||
| Sqrt10 | 求 $\sqrt{10}$【连分数】 | [resolve-sqrt-10](https://hatter.ink/static/resource/muboard/?id=resolve-sqrt-10&version=latest&full=1) |
|
||||
| WallisProduct | 沃里斯乘积 | [wallis-product](https://hatter.ink/static/resource/muboard/?id=wallis-product&version=latest&full=1) |
|
||||
|
||||
|
||||
<br>
|
||||
|
||||
36
src/main/java/me/hatter/math/BinomialTheorem.java
Normal file
36
src/main/java/me/hatter/math/BinomialTheorem.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package me.hatter.math;
|
||||
|
||||
import me.hatter.math.util.MathUtil;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
// https://zhuanlan.zhihu.com/p/34484169
|
||||
public class BinomialTheorem {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(power(BigDecimal.valueOf(0.5), BigDecimal.valueOf(-0.3)));
|
||||
System.out.println(Math.pow(0.5, -0.3));
|
||||
System.out.println(power(BigDecimal.valueOf(0.6), BigDecimal.valueOf(3.1)));
|
||||
System.out.println(Math.pow(0.6, 3.1));
|
||||
}
|
||||
|
||||
public static BigDecimal power(BigDecimal x, BigDecimal u) {
|
||||
return powerWithN(x, u, 100);
|
||||
}
|
||||
|
||||
public static BigDecimal powerWithN(BigDecimal x, BigDecimal u, long n) {
|
||||
BigDecimal result = BigDecimal.ZERO;
|
||||
for (long i = 0; i < n; i++) {
|
||||
BigDecimal ux = BigDecimal.ONE;
|
||||
for (long j = 0; j < i; j++) {
|
||||
ux = ux.multiply(u.subtract(BigDecimal.valueOf(j)));
|
||||
}
|
||||
result = result.add(
|
||||
ux.divide(new BigDecimal(MathUtil.factorial(i)), 100, RoundingMode.HALF_UP)
|
||||
.multiply(x.subtract(BigDecimal.ONE).pow((int) i))
|
||||
);
|
||||
}
|
||||
return result.setScale(100, RoundingMode.HALF_UP);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user