请教一个算法,要求时间越少越好,谢谢! a(n)=-4*((-1)^n)*(1/(-1+2*n))S(n)=a(1)+a(2)+a(3)+...+a(n) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 请教一下如果用a(n)=-4*((-1)^n)*(1/(-1+2*n))S(n)=a(1)+a(2)+a(3)+...+a(n)那么必须遍历所要的足够大的值.即必须从S1----Sn才能计算出有没有什么好的能减少步骤呢?? 64是用高等数学算出来的。Tailor定理。 //Text516用于计算PI要用几个位完成import javax.swing.JOptionPane;import javax.swing.JTextArea;import javax.swing.*;//自定义类用于计算class Caculate{ double Number=0,temp2=0;int i=0,t=0,s=0,u=0;double result=0, temp1=0; Caculate(double temp,double temps){ Number=temp; temp2 =temps;} double Caculation(){ result=0;int s; for(i=1;;i++){ temp1=Math.pow((-1),i+1); t=2*i-1; result=result+(4*temp1)/t; if((int)(Math.PI*Math.pow(10,i)-result*Math.pow(10,i))==0) { s=i; break; } } return(s); } }public class Pi{ public static void main(String args[]){ JTextArea outputArea =new JTextArea(17,30); JScrollPane scroller = new JScrollPane(outputArea);//开辟窗口对话框 double Result; String Temp_num,temp_result="",temp_1=""; int j;double getNumber, m; Temp_num= JOptionPane.showInputDialog("please enter your choose Pi :"); getNumber=Double.parseDouble(Temp_num);//从屏幕中得到近似PI的值并转化为整数 m=(getNumber-(int)getNumber); System.out.println(m);//用于测试是否成功 Caculate c= new Caculate(getNumber,m);//建立新类用于计算 temp_1=" "+c.Caculation()+"\n"; temp_result=temp_result+temp_1; outputArea.setText(temp_result); JOptionPane.showMessageDialog(null,scroller, "The result",JOptionPane.INFORMATION_MESSAGE); System.exit(0); } }有点小问题谁能看看 关于jtable 和JPanel背景图片的问题? 学习java几个月之后关于java可移植性的困惑 关于java缩放 如何监视数据库表中某个字段值的改变? 我两次强制抛出例外!可是SPRING事务并没有回滚!不知道为什么啊!SPRING事物回滚问题 Advice! 如何复制一份ResultSet 贪吃蛇小游戏里面的蛇怎么能自动跑起来 高分求教:多国时区转换的java方法!300分,不封顶!! 系统蓝屏 求助!!关于一个函数库的通用控制函数的的写法问题! 请教画曲线的问题帮忙看看这样行不行
如果用a(n)=-4*((-1)^n)*(1/(-1+2*n))
S(n)=a(1)+a(2)+a(3)+...+a(n)
那么必须遍历所要的足够大的值.
即必须从S1----Sn才能计算出
有没有什么好的能减少步骤呢??
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.*;
//自定义类用于计算
class Caculate{
double Number=0,temp2=0;int i=0,t=0,s=0,u=0;double result=0, temp1=0; Caculate(double temp,double temps){
Number=temp;
temp2 =temps;
}
double Caculation(){
result=0;int s;
for(i=1;;i++){
temp1=Math.pow((-1),i+1);
t=2*i-1;
result=result+(4*temp1)/t;
if((int)(Math.PI*Math.pow(10,i)-result*Math.pow(10,i))==0)
{ s=i;
break;
}
}
return(s);
}
}
public class Pi{
public static void main(String args[]){
JTextArea outputArea =new JTextArea(17,30);
JScrollPane scroller = new JScrollPane(outputArea);//开辟窗口对话框
double Result;
String Temp_num,temp_result="",temp_1="";
int j;double getNumber, m;
Temp_num=
JOptionPane.showInputDialog("please enter your choose Pi :");
getNumber=Double.parseDouble(Temp_num);//从屏幕中得到近似PI的值并转化为整数
m=(getNumber-(int)getNumber);
System.out.println(m);//用于测试是否成功
Caculate c= new Caculate(getNumber,m);//建立新类用于计算
temp_1=" "+c.Caculation()+"\n";
temp_result=temp_result+temp_1;
outputArea.setText(temp_result);
JOptionPane.showMessageDialog(null,scroller,
"The result",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
}
有点小问题谁能看看