# 作業六：尋找尤拉數及圓周率

YTC
c_cpp
2 years ago
1.3 kB
1
Indexable
Never
```//
//  main.cpp
//  codehomework #06 estimate pi and exponential

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main(int argc, const char * argv[]) {
long double E = 0, n;
for(int a =0 ; a <= 5000; a++){
n = 1;
for(int new_n =1 ; new_n <=a ; new_n++){
n = n*new_n;
}
E += 1/n;
}
cout<< "The estimated value of euler number is : "<< setprecision(30)<< fixed <<E << endl;

long double pi1 = 0 , pi2 = 0 ;

//leibniz
for(int n = 0 ; n <=5000; n++){
pi1 += 4*pow(-1,n)/(2*n+1);
}
cout << "#01 -The way that Leibniz used to estimate the value of pi:  " << setprecision(30) << fixed << pi1 << endl;

//euler's way
for(int n = 1 ; n <= 5000 ; n++){
pi2 += 6*1/pow(n,2);
}
cout << "#02 -The way that Euler used to estimate the value of pi:  " << setprecision(30) << fixed << sqrt(pi2) << endl;

//arccos(0)
double pi3;
pi3 = 2*acos(0.000000000000);
cout << "#03 -The way that using arccos to estimate the value of pi: " << pi3 <<endl;

//arcsin(1)
double pi4;
pi4 = 2*asin(1.0000000000000);
cout << "#04 - The way that using arcsin to estimate the value of pi: " << pi4 << endl;

return 0;
}```