编写一个程序来使用Java中的Leibniz系列来估计PI(π) [英] Write a program to estimate PI (π) using the Leibniz series in Java
问题描述
我在网上查了几个小时,想看看能不能找到一个解决方案,而我找到了很多解决方案,我的教授的指示如下:
使用以下序列编写一个程序来估计PI(π)。这个问题在第五章的末尾也被描述为问题5.25。如果你对系列不熟悉,那么问题5.24是一个系列的单一过程,解决方案被发布在作业3课程模块中。
π= 4 *(1-1 / 3 + 1 / 5-1 / 7 + 1 / 9-1 / 11 + ... ... >> -1)^(i + 1)/(2i-1))
显然,这个问题没有用户输入,所以提供了一个修改的工作表。您将编写一个计算PI的程序,使用i值为10,000至100,000,增量为10000.您的输出应该如下所示:(提示:在System.out.println中将i和PI的值放在/ t
i ________ PI
10000 ____ 3.xxxxx
<20000 ____ 3.xxxx
30000 ____ 3.xxxx
您将需要多个循环来执行此操作。外循环将增加i。内部循环将计算从1到i的序列。你可以使用三种类型的循环中的任何一种,或者同时做,或者同时做。
现在我很好意识到除了这个之外,还有很多方法可以更好地发现pi,但是这个任务的重点不是要高效地找到Pi,而是要用循环来练习。然而,我已经尝试了许多方法,他们都返回无限循环,不正确的输出,或者他们只是不编译。
编辑 :感谢Martijn Courteaux我在代码方面做了很大的改进。但是我似乎仍然无法让计数正确地增加。有什么建议么?
我最近的一次尝试如下。
public class LeibnizFormula {
public static void main(String [] args){
System.out.println(i / t Pi);
应该是的。你的内循环有
int count = 10000;
double pi = 0;
双分母= 1;
while(count <100000){
for(int x = 0; x
if(x%2 == 0){
pi = pi +(1 /分母);
}
else {
pi = pi - (1 /分母);
}
分母=分母+ 2;
}
pi = pi * 4;
System.out.println(pi);
count = count + 10000;
System.out.println(count);
现在结果如下:
$ b $ / p>
i / t Pi
3.1414926535900345
20000
12.566037281026608
30000
50.264165790773355
40000
201.05666982975973
50000
804.2266826523694
60000
3216.9067325142446
70000
12867.626931247545
80000
51470.50772578291
90000
205882.03090368543
100000
我不太明白为什么程序工作的时候我硬编码count的值而不是增量的时候。我知道我听起来很乏味,但我真的很想知道发生了什么问题,为什么会出错。
我意识到,简单地发布解决方案在这里,所以我没有要求,我只是需要一些伪代码或指针。我一直在这个工作了很长一段时间。谢谢
解决方案编写这个程序有更高效和简洁的方法,但最简单的解决方法是摆脱
pi = pi * 4
,因为那么while循环的下一个迭代将有一个大得多的值pi
到开始。相反,只需打印pi * 4
而不是pi
。
$ b $另外,这也许并不重要,但是实际上你要比所需要的更精确。例如,对于count = 20000
精度实际上是count = 30000 x <计数
,并且因为每次迭代之后count
增加10000,所以在10000,30000,60000等方面你的确是精确的。你可以修复这个通过重新初始化每次迭代的分母
和pi
的值,或者改变x < count
到x < 10000
。I've looked online for hours trying to see if I could find a solution and while I have found many solutions my instructions from my professor are as follows:
Write a program to estimate PI (π) using the following series. This problem is also described in the text as problem 5.25 at the end of chapter 5. If you are unfamiliar with series, problem 5.24 is a single pass through a series and the solution is posted in the Homework 3 course module.
π=4*(1-1/3+1/5-1/7+1/9-1/11+⋯〖-1〗^(i+1)/(2i-1)) Obviously, there is no user input for this problem so a modified worksheet is provided. You will to write a program that computes PI using i values of 10,000 to 100,000 in increments of 10000. Your output should look like: (Hint: Placing "/t" in the System.out.println between the values of i and PI will give you columns. This is the tab character).i________PI
10000____3.xxxxx
20000____3.xxxx
30000____3.xxxx
You will need multiple loops to do this. The outer loop will increment i. The inner loop will compute the series from 1 to i. You may use any of the three types of loops, for, while, or do-while to do this.
Now I am well aware that there are many ways that are better at finding pi besides this, however the point of this assignment is not to find Pi efficiently but rather to practice with loops. However I've tried numerous approaches and all of them return either infinite loops, incorrect outputs, or they just don't compile.
EDIT: Thanks to Martijn Courteaux I've made a vast improvement in the code. however I still can't seem to get the count to increment properly. Any Suggestions? My Most recent attempt is as follows.
public class LeibnizFormula { public static void main(String[] args) {
System.out.println("i/t Pi"); int count = 10000; double pi = 0; double denominator = 1; while(count < 100000){ for (int x = 0; x < count; x++) { if (x % 2 == 0) { pi = pi + (1 / denominator); } else { pi = pi - (1 / denominator); } denominator = denominator + 2; } pi = pi * 4; System.out.println(pi); count = count + 10000; System.out.println(count); } } }
Now the results are:
i/t Pi 3.1414926535900345 20000 12.566037281026608 30000 50.264165790773355 40000 201.05666982975973 50000 804.2266826523694 60000 3216.9067325142446 70000 12867.626931247545 80000 51470.50772578291 90000 205882.03090368543 100000
I don't really understand why the program is works when I hardcode the values of "count" in but not when increment it. I know I'm sounding tedious but i really want to understand what is going wrong and WHY it's going wrong.
I realize that it's not good form to simply post the solution here so I'm not asking for that, I just need possibly some pseudo code or pointers. I've been working on this for quite a while. Thank you
解决方案There are more efficient and concise ways of writing this program, but the easiest fix would be to get rid of the
pi = pi * 4
, because then the next iteration of the while loop will have a much larger value ofpi
to start. Instead, simply print outpi * 4
instead of justpi
.Also, this may not matter but you are actually giving more precision than is needed; for example, for
count = 20000
the precision is actually whatcount = 30000
should be. Your inner for loop hasx < count
, and sincecount
increases by 10000 after each iteration, you are really getting precision at 10000, 30000, 60000, etc. You can fix this by either reinitializing the values ofdenominator
andpi
each iteration, or changingx < count
tox < 10000
.这篇关于编写一个程序来使用Java中的Leibniz系列来估计PI(π)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!