# cool math homework assignment

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-21-2007
BobMcGee123
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.

Quote:

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;
}

• 01-21-2007
Sang-drax
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.
• 01-22-2007
Happy_Reaper
Further, I believe there are faster converging series to Pi, but none as straightforward as your method, though.
• 01-22-2007
Sang-drax
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.
• 01-22-2007
Sang-drax
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.
• 01-22-2007
BobMcGee123
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.
• 01-22-2007
Sang-drax
Yup, I found that formula on Wikipedia as well.
• 01-23-2007
BobMcGee123
whoa wait what does the double !! mean
• 01-23-2007
Sang-drax
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.
• 01-23-2007
BobMcGee123
Oh, that makes sense. Yeah, I initially thought 'factorial of the factorial' but then that didn't quite make sense.

Gracias!
• 01-23-2007
manutd
I assume the factorial of the factorial would be like: (5!)!
• 01-23-2007
BobMcGee123
We have already established that it is not.
• 01-23-2007
Happy_Reaper
I'd actually never seen that notation before, but it's a fun tidbit of info.
• 01-23-2007
manutd
No, I know 5!! is not factorial of a factorial, but do the parentheses make a difference?
• 01-23-2007
Happy_Reaper
I believe they would. I still think parentheses have a higher precedence than the !! operator.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last