Thread: approximating euler's number

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    approximating euler's number

    Hi,
    I am a newbie and I need some help with this one...

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

    I need to approximate the euler's value, I want to know what should my for loop look like?

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Write it out as a summation and you're halfway there.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    2
    This is what I have, but I am getting a logical error.
    Code:
    n = getN();
    
                for (i = n; i > 0; i--)
                sum += (1/(float) i);
    
                printf( "Sum = %f\n", (sum + 1));
          return 0;
    Last edited by Salem; 10-26-2006 at 05:28 AM. Reason: Added code tags

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Please use code tags when you post code

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > sum += (1/(float) i);
    You want something like
    sum += (1/ factorial(i) );

    Read the formula, then write the code.
    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.

  6. #6
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    As a bookkeeping, I believe that e is actually known as Napier's constant. It was only named e in honor of Euler.
    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

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. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM