ค่าคงที่ตัวเลขโดยประมาณเช่น PI (3.1415926 ... ) และ PHI (1.6180339 ... ) เพื่อความแม่นยำโดยพลการโดยใช้คลาส BigDecimal ของ Java (และวิเคราะห์ผลลัพธ์)
โครงการนี้ได้รับใบอนุญาตภายใต้ใบอนุญาต 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 | นิลกานธา | [2] |
| 3 | นิวตัน | [3] |
| 4 | เวียต | [4] |
| 5 | วอลลิส | [5] |
| 6 | Chudnovsky | [6] |
| 7 | Brent-Salamin (หรือ Gauss-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 เปรียบเทียบการประมาณค่า Runtimes และการใช้หน่วยความจำของอัลกอริทึมที่ใช้ใน Computepi การวนซ้ำโดยการใช้งานพิมพ์ผลลัพธ์ไปยังไฟล์ 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โปรดทราบว่าอัลกอริทึมบางอย่างช้ากว่าในการคำนวณมากกว่าที่อื่น ๆ ตัวอย่างเช่นสูตรของ Viete คำนวณสแควร์รูทสำหรับการวนซ้ำทุกครั้งและช้ากว่าอัลกอริทึมอื่น ๆ
นี่คือตัวอย่างเอาท์พุทของโปรแกรมที่แสดงการเปรียบเทียบการวนซ้ำ 10 ครั้งแรกของอัลกอริทึมที่ดำเนินการทั้งหมด 7 รายการ ส่วนแรกแสดงอาร์กิวเมนต์ที่ส่งผ่านไปยัง ComputePi (พร้อมข้อมูลการใช้งานรันไทม์และหน่วยความจำ) และส่วนที่สองแสดงตารางที่มีจำนวนตัวเลขนำที่แม่นยำของ 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 | นิลกานธา | นิวตัน | เวียต | วอลลิส | 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 |