# Thread: cool math homework assignment

1. ## cool math homework assignment

Well you can move this to whatever board you want. Anyway I'm taking a course called 'engineering mathematics.' It's kind of the equivalent of a linear algebra course. My professor is an eccentric old lady that is way too smart to teach math to humans.

Our assignment over the weekend was to write in excel a command to evaluate the series for 'e' and 'pi' and to see how many terms in the series it took to get a certain number of decimal places accuracy. She suggested trying a couple of hundred terms, so of course I wrote a program that evaluates PI using 50 million terms. Kind of neat. Here are the results, the series for 'e' converged pretty quickly, not true for PI.

E after 20 terms in the series: 2.718281828459045500000000000000

PI after 1000 terms in series: 3.142593654340044100000000000000

PI after 50000000 terms in series: 3.141592673590250900000000000000

Here's the program for it
Code:
```#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;

double	factorial(double	input)
{
if(input > 0)
return	input * factorial(input-1);
else	return	1;
}

double	e_term(double	which_term)
{
return	1.0 / (factorial(which_term));
}

double	pi_term(double which_term)
{
int	i_term;
_asm
{
fld		which_term;
fistp	i_term;
}

double	sgn			=	pow(-1.0,i_term+1);
double	denom		=	1.0 / ((2.0 * which_term)-1);
return	4 * sgn * denom;
}

int main(void)
{
std::ofstream	fout;
fout.open("results.txt");
fout.precision(50);
cout.precision(50);

double	summation = 0.0;
for(double	which_term = 0; which_term < 20; ++which_term)
{
summation += e_term(which_term);
fout << summation << "\n";
}

fout << "\n\n\n\n\n\n\n";

summation = 0;
for(which_term = 1; which_term < 1000; ++which_term)
{
summation += pi_term(which_term);
}
cout << "PI after 1000 terms in series: " << summation << "\n";
fout << "\nPI after 1000 terms in series: " << summation << "\n\n";

summation = 0;
for(which_term = 1; which_term < 50000000; ++which_term)
{
summation += pi_term(which_term);
}

cout << "PI after 50000000 terms in series: " << summation << "\n";
fout << "\nPI after 50000000 terms in series: " << summation << "\n\n";
return	0;
}``` 2. Your series for e will converge extremly fast because the terms get really small very fast (factorial in the denominator).

Since the series you're using for pi is an alternating one, it is very easy to give an estimation how close to pi you are. After the first term your estimation is too big and after the second one it is too small and so on. So an upper bound of the error is the last term you added. That way you can know for sure how many digits are correct. 3. Further, I believe there are faster converging series to Pi, but none as straightforward as your method, though. 4. Here's a cool way of calculating pi:

*Select two random numbers in the interval [-1...1]
*Count the number of times you end up within the unit disc. sqrt(x^2 + y^2) <= 1
*The ratio between this number and the total amound tends to the ratio of the unit disc to the area of the square (4).

Code:
```double calculatePi(int nTimes)
{
double nWithin = 0;
for (int n=1; n<= nTimes; ++n) {
double x = random(-1,1);
double y = random(-1,1);
if (x*x + y*y <= 1)
nWithin++;
}
return 4 * (nWithin/nTimes);
}```
This method converges very slowly. 5. And here is a good method of calculating pi:

Code:
```#include <iostream>
#include <iomanip>
using namespace std;

double calculatePi(int nTimes)
{
double halfpi = 1; //First term is 1
double numerator = 1;
double denominator = 1;

for (double k=1; k<= nTimes; ++k)
{
numerator *= k;
denominator *= 2*k + 1;
halfpi += numerator/denominator;
}
return 2 * halfpi;
}

int main()
{
cout << "Pi is approximately " << setprecision(15) << calculatePi(10) << " with 20 terms." << endl;
cout << "Pi is approximately " << calculatePi(50) << " with 50 terms." << endl;
cin.get();
}```
The results:
Code:
```Pi is approximately 3.14110602160138 with 20 terms.
Pi is approximately 3.14159265358979 with 50 terms.```
The formula is attached as an image. 6. Yeah, I saw the method that you suggested, I think on wikipedia. Glad you posted the source. She wanted us to use this particular method to demonstrate the idea in Sang drax's first reply, that the estimate goes above and then below, oscillating (it tied into this week's lecture).

Thanks for the replies. 7. Yup, I found that formula on Wikipedia as well. 8. whoa wait what does the double !! mean 9. One would think that it means the factorial of the factorial, but it doesn't. It means that only every other factor should be used in the product.
5!! = 5 * 3 * 1
6!! = 6 * 4 * 2

In this case, both the factorial and the !! is calculated very quickly using the previous term cleverly. 10. Oh, that makes sense. Yeah, I initially thought 'factorial of the factorial' but then that didn't quite make sense.

Gracias! 11. I assume the factorial of the factorial would be like: (5!)! 12. We have already established that it is not. 13. I'd actually never seen that notation before, but it's a fun tidbit of info. 14. No, I know 5!! is not factorial of a factorial, but do the parentheses make a difference? 15. I believe they would. I still think parentheses have a higher precedence than the !! operator. Popular pages Recent additions 