Calculating the Value of π

 

Calculateghe value of π from the infinite series(Gegory Leibniz series):
π=4-4/3+4/5 -4/7+4/9-4/11+...


a) Design and code a main method to print a table that shows the value of π for the first 15 terms of this series (display the value with 12 places after the decimal point:%12.10f ).

Example of Table for first 12 terms 
 
#Terms Calculated Pi
1  4.0000000000
2  2.6666666667
... ...
12 3.0584027659


b) Then design and add code statements to the main method to print a second table that shows the calculated value of π using this series for up to 1000,000 terms using a table interval of
50,000.
Add a third column showing the percentage error from Math PI :(Math.PI-pi) /Math.PI *100.0

 

Example of Table_for 1 Million terms

 Hint: Use an if statement and the remainder operator % to print only every 50,000 calculations. 

# Terms   Calculated Pi     Error(%)
50000      3.1415726536   0.000637%
100000    3 .1415826536  0.0003185%

... ...  ...
1000000  3 1415916536   0.0000324%


c) Based on the code in a) and/or b)above,create a separate method called calcPi that accepts the number of terms as a parameter and returns the value calculated for Pi.Select the appropriate return type for calcPi.The public static calcPi method must not print out anything or request input from the user.


d)Print the same table as in b) above by adding code to main to call he calcPi method using an appropriate loop.You now should be printing 3 separate tables for parts a),b) and c).


e)Write a method public static int calcTargetPercent(double accuracyPercent) that accepts a single parameter specifying the accuracy as a percentage of Math.PI,and returns the number of terms required to reach the required accuracy.Test your method for a value of 0.10%.


f) Optional: Determine using code how many terms of this series must be used before you first get value of π accurate to 5 decimal places i.e.3.14159? Create a loop to stop automatically when this value is reached and then display the value and the error relative to Math.PI.

 

源代码:

 

#include<stdio.h>
#define MAX1 15
#define MAX 1000000
#define PI  acos(-1.0)
double calcPi(int M){
 double s=0.0;
 int i0;
 //改变循环次数
 for(i0=1;i0<=M;i0++){
  if(i0%2==0){
   s-=1.0/(2*i0-1);
   }
  else{
  s+=1/(2.0*i0-1);
  }
  }
  return 4*s;
 }
double targetPercent(int MA){
 double Pi0,errorPi;
 int j0;
 for(j0=50000;j0<=MA;j0+=50000){
 printf("%d  ",j0);
 Pi0=calcPi(j0);   //计算近似π值
 errorPi=fabs((PI-Pi0)/PI*100.0);
 printf("%.6lf%%\n",errorPi);
 }
}
int calcTargetPercent(double accuracyPercent){
 double Pi2,error2;
 double sum2=0.0;
    int i2;//作为计算项数的标志
 for(i2=1;;i2++){
  if(i2%2==0){
   sum2-=1.0/(2*i2-1);
   }
   else{
  sum2+=1/(2.0*i2-1);
  }
     error2=fabs((PI-4*sum2)/PI*100.0);
  if(error2<=accuracyPercent){
     return i2;}
 }
}
main(){
printf("打印前15项的近似π值:\n");
 double sum=0.0;
 int i;
 //改变循环次数
 for(i=1;i<=MAX1;i++){
  printf("%d   ",i);
  if(i%2==0){
   sum-=1.0/(2*i-1);
   }
  else{
  sum+=1/(2.0*i-1);
  }
        printf("%12.10lf\n",4*sum);
 }
printf("\n");
printf("50000次一个循环,打印项数、π值和失误率:\n");
 double sum0=0.0;
 double Error;
 int k;
 //改变循环次数
 for(k=1;k<=MAX;k++){
  if(k%2==0){
   sum0-=1.0/(2*k-1);
   }
  else{
  sum0+=1/(2.0*k-1);
  }
  if(k%50000==0){
  //50000次一个循环
  Error=fabs((PI-4*sum0)/PI*100.0);
        printf("%d   ",k);
  printf("%12.10lf   ",4*sum0); 
  printf("%.6lf%%\n",Error);
}   
 }
 printf("\n"); 
 printf("打印表a):\n");
double Pi;
 int j;
 for(j=1;j<=15;j++){
 printf("%d  ",j);
 Pi=calcPi(j);
 printf("%12.10lf\n",Pi);}
printf("\n");
printf("打印表b):\n");
double Error0;
Error0=targetPercent(1000000);
printf("\n");
printf("打印表c):\n");
 double Pi1,errorPi0;
 int j1;
 for(j1=50000;j1<=1000000;j1+=50000){
 if(j1%50000==0){
 printf("%d  ",j1);
 Pi1=calcPi(j1);
 printf("%12.10lf  ",Pi1);
 errorPi0=fabs((PI-Pi1)/PI*100.0);
 printf("%.6lf%%\n",errorPi0);
 }   }
printf("\n");
printf("当误差率为0.10时,打印项数: \n");
printf("%d",calcTargetPercent(0.10));
printf("\n\n");
printf("当近似π值精确5位时,打印项数、π值和失误率: \n");
double sum3=0.0;
double error3;
int i3;
for(i3=1;;i3++){
  if(i3%2==0){
   sum3-=1.0/(2*i3-1);
  }
  else{
  sum3+=1/(2.0*i3-1);
  }
  if((int)((sum3*4)*100000)==314159){
   printf("%d   ",i3);
 error3=fabs((PI-4*sum3)/PI*100.0);
 printf("%12.11lf  ",4*sum3);
    printf("%.6lf%%",error3);
 break;
   }
 }
}

截图与单独代码:

 

a

代码:

#include<stdio.h>

#define MAX1 15

main(){

double sum=0.0;

int i;

//改变循环次数

for(i=1;i<=MAX1;i++){

printf("%d   ",i);

if(i%2==0){

 sum-=1.0/(2*i-1);

 }

else{

sum+=1/(2.0*i-1);

}

        printf("%12.10lf\n",4*sum);

}

}

 

运行结果:

 

b

代码:

#include<stdio.h>

#include<math.h>

#define MAX 1000000

#define PI  acos(-1.0)

main(){

double sum0=0.0;

double Error;

int k;

//改变循环次数

for(k=1;k<=MAX;k++){

if(k%2==0){

 sum0-=1.0/(2*k-1);

 }

else{

sum0+=1/(2.0*k-1);

}

if(k%50000==0){

//50000次一个循环

Error=fabs((PI-4*sum0)/PI*100.0);

        printf("%d   ",k);

printf("%12.10lf   ",4*sum0);

printf("%.6lf%%\n",Error);

}    

 }   

}

 

运行结果:

 

c

代码:

double calcPi(int MAX){

double sum=0.0;

int i;

//改变循环次数

for(i=1;i<=MAX;i++){

if(i%2==0){

 sum-=1.0/(2*i-1);

 }

else{

sum+=1/(2.0*i-1);

}

}

return 4*sum;

}

 

无输入输出

d

 

 

运行结果与a,b一样

 

打印表a代码:

#include<stdio.h>

double calcPi(int M){

double s=0.0;

int i0;

//改变循环次数

for(i0=1;i0<=M;i0++){

if(i0%2==0){

 s-=1.0/(2*i0-1);

 }

else{

s+=1/(2.0*i0-1);

}

}

return 4*s;

}

main(){

double Pi;

int j;

for(j=1;j<=15;j++){

printf("%d  ",j);

Pi=calcPi(j);

printf("%12.10lf\n",Pi);}

}

 

 

打印表c代码:

#include<stdio.h>

#include<math.h>

#define PI acos(-1.0)

double calcPi(int MAX){

double sum=0.0;

int i;

//改变循环次数

for(i=1;i<=MAX;i++){

if(i%2==0){

sum-=1.0/(2*i-1);

 }else{

sum+=1/(2.0*i-1);

}

}

return 4*sum;

}

main(){

double Pi,errorPi;

int j;

for(j=50000;j<=1000000;j+=50000){

if(j%50000==0){

printf("%d  ",j);

Pi=calcPi(j);

printf("%12.10lf  ",Pi);

errorPi=fabs((PI-Pi)/PI*100.0);

printf("%.6lf%%\n",errorPi);

}  }        }

 

打印表b代码:

#include<stdio.h>

#include<math.h>

#define PI acos(-1.0)

double calcPi(int M){

double s=0.0;

int i0;

//改变循环次数

for(i0=1;i0<=M;i0++){

if(i0%2==0){

 s-=1.0/(2*i0-1);

 }

else{

s+=1/(2.0*i0-1);

}

}

return 4*s;

}

double targetPercent(int MA){

double Pi0,errorPi;

int j0;

for(j0=50000;j0<=MA;j0+=50000){

printf("%d  ",j0);

Pi0=calcPi(j0);   //计算近似π值

errorPi=fabs((PI-Pi0)/PI*100.0);

printf("%.6lf%%\n",errorPi);

}

}

main(){

double Error0;

Error0=targetPercent(1000000);

}

运行结果:

 

e

代码:

#include<stdio.h>

#include<math.h>

#define PI acos(-1.0)

int calcTargetPercent(double accuracyPercent){

double Pi,error;

double sum=0.0;

    int i;//作为计算项数的标志

for(i=1;;i++){

if(i%2==0){

 sum-=1.0/(2*i-1);

 }

 else{

sum+=1/(2.0*i-1);

}

    error=fabs((PI-4*sum)/PI*100.0);

if(error<=accuracyPercent){

   return i;}

}

}

main(){

    printf("%d",calcTargetPercent(0.10));

}

运行结果:

 

f

代码:

#include<stdio.h>

#include<math.h>

#define PI acos(-1.0)

main(){

double sum=0.0;

double error;

int i;

for(i=1;;i++){

  if(i%2==0){

 sum-=1.0/(2*i-1);

  }

  else{

sum+=1/(2.0*i-1);

  }

  if((int)((sum*4)*100000)==314159){

   printf("%d   ",i);

error=fabs((PI-4*sum)/PI*100.0);

printf("%12.11lf  ",4*sum);

    printf("%.6lf%%",error);

break;

   }

 }

}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值