cool math homework assignment

This is a discussion on cool math homework assignment within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Well you can move this to whatever board you want. Anyway I'm taking a course called 'engineering mathematics.' It's kind ...

  1. #1

    Join Date
    May 2005
    Posts
    1,041

    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;
    }
    I'm not immature, I'm refined in the opposite direction.

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Further, I believe there are faster converging series to Pi, but none as straightforward as your method, though.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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.
    Attached Images Attached Images  
    Last edited by Sang-drax; 01-22-2007 at 11:15 AM.

  6. #6

    Join Date
    May 2005
    Posts
    1,041
    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.
    I'm not immature, I'm refined in the opposite direction.

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Yup, I found that formula on Wikipedia as well.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8

    Join Date
    May 2005
    Posts
    1,041
    whoa wait what does the double !! mean
    I'm not immature, I'm refined in the opposite direction.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10

    Join Date
    May 2005
    Posts
    1,041
    Oh, that makes sense. Yeah, I initially thought 'factorial of the factorial' but then that didn't quite make sense.

    Gracias!
    I'm not immature, I'm refined in the opposite direction.

  11. #11
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I assume the factorial of the factorial would be like: (5!)!
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  12. #12

    Join Date
    May 2005
    Posts
    1,041
    We have already established that it is not.
    I'm not immature, I'm refined in the opposite direction.

  13. #13
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I'd actually never seen that notation before, but it's a fun tidbit of info.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  14. #14
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    No, I know 5!! is not factorial of a factorial, but do the parentheses make a difference?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  15. #15
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    I believe they would. I still think parentheses have a higher precedence than the !! operator.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. please help..linked list homework assignment due tomorrow..
    By rocketman03 in forum C Programming
    Replies: 2
    Last Post: 11-23-2008, 05:32 PM
  4. Please help me with a math (trig) assignment: finding angle
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 03-26-2003, 04:17 PM
  5. Trying to be cool
    By Theologian in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 08-19-2001, 06:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21