Euler's Number

This is a discussion on Euler's Number within the C++ Programming forums, part of the General Programming Boards category; I need help on writing a C++ program to compute Euler's number it being e= 1/0! + 1/1! + 1/2! ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    61

    Cool Euler's Number

    I need help on writing a C++ program to compute Euler's number it being

    e= 1/0! + 1/1! + 1/2! + ... + 1/n!

    Please help I am new at the programming scene I'd greatly appreciate any feedback

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Seems like homework to me. Tell us what you've done, what you think you can do, pseudo-code, etc.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Its not particularly difficult, you need two functions: 1 - something to compute a factorial (simple), and 2 - something to compute the number. I wrote a program that should be working, but for some reason my computer's rounding everything to ints before it adds them =(

    I'm a bit confused why a float var type would be becoming an int ?

    Good luck!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Code:
    return std::exp(1);
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Junior89 View Post
    Its not particularly difficult, you need two functions: 1 - something to compute a factorial (simple), and 2 - something to compute the number. I wrote a program that should be working, but for some reason my computer's rounding everything to ints before it adds them =(

    I'm a bit confused why a float var type would be becoming an int ?

    Good luck!
    No real need to write a factorial function. In fact doing so would probably slow it down.

    When doing this problem the first thing to realize is that n will always be greater then or equal to 0. As such 1/0! will always be done. So it is a good idea to initialize based off of that.

    So we create a double to store e and give it the value of 1.0 and create an unsigned int to store the denominator and initialize it to 1 (0! is defined to 1 for this problem).

    Walk though the steps for n=3:
    n=0: e=1.0 d=1
    n=1:
    d = d * 1 (1! is 1 * 0!)
    e = e + 1.0 / d

    n=2:
    d = d * 2 (2! is 2 * 1!)
    e = e + 1.0 / d

    n=3:
    d = d * 3 (3! is 3 * 2!)
    e = e + 1.0 / d

    The higher the value of n the more precision you gain.

    I hope you can see now the loop in this problem.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    61

    Cool

    So now I just put in a while for some n value?

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by Junior89 View Post
    Its not particularly difficult, you need two functions: 1 - something to compute a factorial (simple), and 2 - something to compute the number. I wrote a program that should be working, but for some reason my computer's rounding everything to ints before it adds them =(

    I'm a bit confused why a float var type would be becoming an int ?

    Good luck!
    A factorial function would be inefficient. Notice that the denominator is always the factorial of the next higher number. This means you should just keep a running product and multiply the next factor in on every iteration.

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by BJtoVisualcC++ View Post
    So now I just put in a while for some n value?
    while, for, do while, take your pick

    I think I used a for but thats mainly personal preference.

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    61

    what I have

    see attachment
    Attached Files Attached Files

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I can't open attachments on this computer, and I'm sure some people don't feel like clicking on the link, either. Since it's only 621 bytes you might as well just post it in [code] tags.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    Quote Originally Posted by Desolation View Post
    Seems like homework to me. Tell us what you've done, what you think you can do, pseudo-code, etc.
    did you see my attachment

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by BJtoVisualcC++ View Post
    did you see my attachment
    Obviously they didn't since you posted the attachment after they posted.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    After looking at your code I can see you totally ignored my explanation of the problem.

    Let me ask these two questions:
    What is the value of e?
    Can a variable of type long store the value of e?

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > return factX;
    > cout << "The Euler's number is " << factX << endl;
    Remove the return, it is stopping you printing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <cmath>
    
    using namespace std;  //I would avoid this. If your teacher disagrees, strangle him.
    
    int main( )
    {
       int n;            //Initializing counter      //fine, but for clarity you might want to save it for later
       long factX = 1;  //setting factorial to start at 1       //e is not a type of integer! Use double.
       n = 0;          //setting counter to zero          //might as well do this when you declare the variable, might as well within the loop
    
       for(n = 1, factX =1; n<= 10; n++)  factX = factX * n;
    
       return factX;  //return to the caller? Uh oh...
    
       cout << "The Euler's number is... doesn't matter because this never executes...  " << endl;
    
       system("pause");
       return 0;
    }
    Just think about what you're telling the computer to do:

    1. Allocate space for an integer. Call it "n".

    2. Allocate space for a long integer. Call it factX. Initialize its value to the integral constant, 1.

    3. Set n to the integral constant, 0.

    4. Set n to 1. Set factX to 1. Until n is not less than or equal to 10, execute the below code, each time checking first to see if n is still less than or equal to 10. Increase n by 1 each time, after the below code is executed.

    4a (The "below" code): set the value of factX so that it is equal to the product of itself and n (this will be an integer).

    5. Now that the loop is done, get ready to return to the system process that called this program and give it the value we were calling factX. Clean up. End execution.

    Is this what you want to program to do? Write out in prose what you want done, so that a computer might understand it. Then make code.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

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

Similar Threads

  1. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 01:03 AM
  2. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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