Java-Pi的Ramanujan系列 [英] Java - Ramanujan Series for pi

查看:104
本文介绍了Java-Pi的Ramanujan系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我在计算机科学课程中的一个程序,我必须使用以下公式来计算pi的值,我在使用Java中的数学方程式时遇到了麻烦.

For one of my programs in my Computer Science class I have to calculate the value of pi using the following formula, I'm having trouble with the math equation in java.

这是数学公式: 我使用的公式是:

for (int i = 0; i < k; i++) 
{
   term = ((calculate(4*i)*(1103+(26390*i)))/(Math.pow(calculate(i), 4))*Math.pow(396, (4*i)));
   sum += term;
}
sum *= ((2*Math.sqrt(2))/9801);
sum = Math.pow(sum, -1);

for循环应计算sigma符号及其右边的所有内容. 下一部分应将该数字乘以2的2的平方根除以9801,最后一部分应将该数字乘以-1的幂,因为等式找到1/pi,这应该使小数倒数.

The for loop should calculate the sigma sign and everything to the right of it. The next part should multiply that number by 2 square roots of 2 divided by 9801 and the final part should take that number to the -1st power, since the equation finds 1/pi this should reciprocate the fraction.

calculate方法只是找到一个数字的阶乘.

the calculate method just finds the factorial of a number.

当我运行程序时,最终的答案恰好是pi在1处,2.1355575437204093E-13在2处,并且它一直打印错误的数字.知道为什么这会给出错误的答案吗?

When I run the program, the final answer is exactly pi at 1, 2.1355575437204093E-13 at 2, and it keeps printing wrong numbers. Any idea why this could be giving the wrong answer?

谢谢,感谢您的帮助!

推荐答案

分母的括号错误:

term = (double)(calculate(4*i)*(1103+(26390*i)))/((Math.pow(calculate(i), 4))*Math.pow(396, (4*i)));

尝试使用双精度进行计算.值k = 30,结果为3.141592653589793

Try using doubles for your calculations. And with a value of k = 30, the result is 3.141592653589793

确定PI的值就像应用随机方法来找到确定性值,使用大的迭代可以提供更好的精度

And determining the value of PI is like applying stochastics to find a deterministic value, using large iterations gives a better precision

这篇关于Java-Pi的Ramanujan系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆