NumericConstantApproximator
1.0.0
使用Java的BigDecimal類(例如PI(3.1415926 ...)和PHI(1.6180339 ...))近似值數字常數(3.1415926 ...)使用Java的BigDecimal類(並分析結果)。
該項目已根據MIT許可獲得許可。
注意:此項目需要至少要運行Java 14。建議使用Java 15,因為它是本文檔撰寫的最新版本。
javac ComputePhi.java ComputePi.java PiAlgorithmExaminer.java
Computephi以算法的形式證明,無論您從哪個術語開始,如果您通過添加兩個上一個術語來生成一系列數字,則連續項之間的比率收斂到PHI(黃金比率)。
java ComputePhi <first-term> <second-term> <iterations> <precision> [options]
--print_steps (-p) # Print approximation for each iteration of algorithm.
--compare_values (-c) # Compare sequential iterations' results, and stop iterating early if they are equal. # The Fibonacci sequence
java ComputePhi 1 1 10 10
# The Lucas numbers (printing each step)
java ComputePhi 2 1 10 10 -p
# Go wild if you want to!
java ComputePhi 23 9001 200 1000 --print_steps
java ComputePhi 314 159 200 20000 --compare_valuesComputepi演示了7種近似PI的不同算法。然後將結果與一百萬位PI(從此處來源)的一百萬個數字的預算文件進行比較,以確定準確性。
java ComputePi <algorithm> <iterations> <precision> [options]
<algorithm>可以是一個數字,也可以是算法名稱(不敏感):| 數字 | 算法名稱 | 參考 |
|---|---|---|
| 1 | Gregory-Leibniz | [1] |
| 2 | Nilakantha | [2] |
| 3 | 牛頓 | [3] |
| 4 | 越野 | [4] |
| 5 | 瓦利斯 | [5] |
| 6 | Chudnovsky | [6] |
| 7 | 布倫特 - 六鹽(或高斯 - legendre) | [7] |
--all_digits (-a) # Print all digits (default only prints accurate digits).
--print_steps (-p) # Print approximation for each iteration of algorithm.
--compare_values (-c) # Compare sequential iterations' results, and stop iterating early if they are equal.
--estimate_memory_usage (-e) # Print an estimate of memory usage.
java ComputePi 1 1000 10
java ComputePi 2 1000 10 --print_steps
java ComputePi CHUDNOVSKY 10 100 --compare_values --estimate_memory_usage pialgorithmexaminer比較了在computepi itseration-tiretration中實現的算法的近似,運行時間和內存使用情況,將結果打印到file output.txt 。
java PiAlgorithmExaminer <algorithms> <iterations> <precision> [options]
<algorithms>是算法名稱和數字的逗號分隔列表(有關值,請參見上文)。 --skip_tests (-s) # Skip running the tests (only use to re-analyze already generated output data).
--print_table (-p) # Print the analysis as a table (defaults to printing results in a "key:value" format, which is more useful for large datasets). java PiAlgorithmExaminer CHUDNOVSKY,BRENT_SALAMIN 1000 2000
java PiAlgorithmExaminer 1,2,3,4,5,6,7 10 2000 --print_table # <----- outputs results shown below請注意,某些算法比其他算法要慢得多。例如,越南公式為每種迭代計算一個平方根,因此比其他算法要慢。
這是該程序的示例輸出,其中顯示了所有7種實施算法的前10個迭代的比較。第一部分顯示了傳遞給ComputePi參數(帶有運行時和內存使用信息),第二部分顯示了一個表,其中具有PI PI的準確領先數字數量。
GREGORY_LEIBNIZ 10 2000 --print_steps --compare_values --estimate_memory_usage 0.103 seconds 1.640 KB
NILAKANTHA 10 2000 --print_steps --compare_values --estimate_memory_usage 0.027 seconds 1.652 KB
NEWTON 10 2000 --print_steps --compare_values --estimate_memory_usage 0.096 seconds 3.263 KB
VIETE 10 2000 --print_steps --compare_values --estimate_memory_usage 0.395 seconds 10.548 KB
WALLIS 10 2000 --print_steps --compare_values --estimate_memory_usage 0.017 seconds 1.647 KB
CHUDNOVSKY 10 2000 --print_steps --compare_values --estimate_memory_usage 0.133 seconds 13.305 KB
BRENT_SALAMIN 10 2000 --print_steps --compare_values --estimate_memory_usage 0.331 seconds 11.368 KB
| 迭代 | gregory_leibniz | Nilakantha | 牛頓 | 越野 | 瓦利斯 | Chudnovsky | brent_salamin |
|---|---|---|---|---|---|---|---|
| 1 | 0 | 2 | 1 | 0 | 0 | 14 | 0 |
| 2 | 1 | 2 | 3 | 1 | 0 | 28 | 3 |
| 3 | 0 | 3 | 3 | 2 | 0 | 42 | 8 |
| 4 | 1 | 2 | 4 | 2 | 0 | 56 | 19 |
| 5 | 0 | 3 | 4 | 3 | 1 | 70 | 41 |
| 6 | 1 | 3 | 6 | 4 | 1 | 85 | 84 |
| 7 | 1 | 3 | 6 | 5 | 1 | 99 | 171 |
| 8 | 1 | 4 | 7 | 5 | 1 | 113 | 345 |
| 9 | 1 | 4 | 8 | 5 | 1 | 127 | 694 |
| 10 | 1 | 4 | 9 | 6 | 1 | 142 | 1392 |