From 612ae3098bba7fb2a5425d69ec0c1df411c59acc Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Fri, 18 Jul 2025 21:26:09 +0800 Subject: [PATCH] feat: add wallis product --- README.md | 13 +++++----- .../java/me/hatter/math/WallisProduct.java | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 src/main/java/me/hatter/math/WallisProduct.java diff --git a/README.md b/README.md index 585fd26..a7e8368 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ # math -| Class | Comments | MuBoard Link | -|--------------------------|--------------------|-----------------------------------------------------------------------------------------------------------------------------| -| ChineseRemainderTheorem | 中国剩余定理 | [chinese-remainder-theorem](https://hatter.ink/static/resource/muboard/?id=chinese-remainder-theorem&version=latest&full=1) | -| EulerTheorem | 欧拉定理 | [euler-theorem](https://hatter.ink/static/resource/muboard/?id=euler-theorem&version=latest&full=1) | -| ModularInv | 求乘法逆元【扩展欧几里得法】 | - | -| Sqrt10 | 求 $\sqrt{10}$【连分数】 | [resolve-sqrt-10](https://hatter.ink/static/resource/muboard/?id=resolve-sqrt-10&version=latest&full=1) | +| Class | Comments | MuBoard Link | +|-------------------------|--------------------|-----------------------------------------------------------------------------------------------------------------------------| +| ChineseRemainderTheorem | 中国剩余定理 | [chinese-remainder-theorem](https://hatter.ink/static/resource/muboard/?id=chinese-remainder-theorem&version=latest&full=1) | +| EulerTheorem | 欧拉定理 | [euler-theorem](https://hatter.ink/static/resource/muboard/?id=euler-theorem&version=latest&full=1) | +| ModularInv | 求乘法逆元【扩展欧几里得法】 | - | +| 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) | diff --git a/src/main/java/me/hatter/math/WallisProduct.java b/src/main/java/me/hatter/math/WallisProduct.java new file mode 100644 index 0000000..6b1fa07 --- /dev/null +++ b/src/main/java/me/hatter/math/WallisProduct.java @@ -0,0 +1,26 @@ +package me.hatter.math; + +import java.math.BigDecimal; +import java.math.RoundingMode; + +public class WallisProduct { + + public static void main(String[] args) { + // 3.1415925750499739534746260502048989270653181072075621931977986489848855062212872415726332946132240849 + System.out.println(pi()); + } + + public static BigDecimal pi() { + BigDecimal p = BigDecimal.valueOf(2); + for (long i = 1; i < 10000000; i++) { + BigDecimal doubleI = BigDecimal.valueOf(i * 2); + p = p.multiply( + doubleI.divide(doubleI.subtract(BigDecimal.ONE), 100, RoundingMode.FLOOR) + ).multiply( + doubleI.divide(doubleI.add(BigDecimal.ONE), 100, RoundingMode.FLOOR) + ); + p = p.divide(BigDecimal.ONE, 100, RoundingMode.FLOOR); + } + return p; + } +}