**5.25(计算PI)使用下面的数列可以近似计算PI: 编写程序,显示当i=10000,20000,…,100000时PI的值。 **5.25(Compute p) You can approximate p by using the following summation: Write a program that displays the PI value for i = 10000, 20000, …, and 100000.
参考代码:
package chapter05;publicclassCode_25{publicstaticvoidmain(String[] args){double number =0;for(int i =1;i <=100000;i++){
number += Math.pow(-1, i+1)/(2* i -1);if(i %10000==0)
System.out.println("The PI value for i = "+ i +" is "+ number *4);}}}
结果显示:
The PI value for i =10000 is 3.1414926535900345
The PI value for i =20000 is 3.1415426535898248
The PI value for i =30000 is 3.141559320256462
The PI value for i =40000 is 3.1415676535897985
The PI value for i =50000 is 3.1415726535897814
The PI value for i =60000 is 3.141575986923102
The PI value for i =70000 is 3.141578367875482
The PI value for i =80000 is 3.1415801535897496
The PI value for i =90000 is 3.1415815424786238
The PI value for i =100000 is 3.1415826535897198
Process finished with exit code 0
第五章第二十五题(计算PI)(Compute PI)**5.25(计算PI)使用下面的数列可以近似计算PI:编写程序,显示当i=10000,20000,…,100000时PI的值。**5.25(Compute p) You can approximate p by using the following summation:Write a program that displays the PI value for i = 10000, 20000, …, and 100000.参考代码: