Euler's number question

This is a discussion on Euler's number question within the C Programming forums, part of the General Programming Boards category; Hello, I am having a problem with my following lab assignment. Iíve included what Iíve completed so far. I am ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    6

    Unhappy Euler's number question

    Hello, I am having a problem with my following lab assignment. Iíve included what Iíve completed so far. I am not sure if it is correct. If anyone can help me out, I would greatly appreciate it.

    This is the problem in question:

    Euler's number, "e" is used as the basic of natural logarithms. It can be approximated using the following formula:

    e = 1 + 1/1! + 1/2! + 1/3! + 1/4! 1/5! + 1/6! + ... + 1/(n-1)! + 1/n!

    Write a program that approximates "e" using a loop that terminates when difference between two successive values of "e" differ by less than 0.0000001.

    This is what Iíve started/completed so far.

    ***start***

    main()
    {
    /*repeat until euler(n) - euler(n-1)<=0.0000001 */
    }

    float euler(int n)
    {
    if(n==0)
    return 1;
    else
    return (1/factorial(n) + euler(n-1));
    }

    int factorial(int n)
    {
    .
    .
    .
    }

    ***is this correct?***

    euler(0) => 1

    euler(1) => 1/1! + euler(0)
    1/1! + 1

    euler(2) => 1/2! + euler(1)
    1/2! + 1/1! + 1

    euler(3) => 1/3! + euler(2)
    1/3! + 1/2! + 1/1! + 1

    euler(4) => 1/4! + 1/3! + 1/2! + 1/1! + 1
    1/4! + euler(3)

    ***end***

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Don't do it recursivly. Have a running sum, add 1/k! to the sum and then if 1/k! is less than 0.0000001 stop else repeat.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    6
    I am still not sure how to pill this all together...

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    It is correct. I understand you need help with the factorial? I saw you understand recursion, so the factorial shouldn't be too hard.

    Factorial:

    n! = n x (n - 1) x (n - 2) x ... x 3 x 2 x 1

    So the end-step is 1 and the recursion step is n x (n - 1).

    Code:
    int fac (int n)
    {
        if (n == 1)
            return 1;
        else
            return (n * fac (n - 1));
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. flipping number question..
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 12-09-2008, 08:02 AM
  2. Euler's number
    By llf in forum C++ Programming
    Replies: 1
    Last Post: 05-05-2007, 05:51 PM
  3. help with number menu
    By j4k50n in forum C Programming
    Replies: 12
    Last Post: 04-17-2007, 01:33 AM
  4. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  5. Question about limiting the number of user inputs in C
    By chobibo in forum C Programming
    Replies: 15
    Last Post: 08-30-2006, 12:37 AM

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