Thread: Need suggestion on sum of factorials.

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    10

    Question Need suggestion on sum of factorials.

    Here is the problem:

    Write a program to solve to following expression:
    S=(1!/2)+(2!/4)+(3!/6)+....+(N!/2N)

    - N read from keyboard.

    This is my solution:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    long inputNumber, x;
    double fact=1, sum=0;
    
    void main()
    {
        clrscr();
        printf("Enter number: ");
        scanf("%ld", &inputNumber);
    
        if(inputNumber==0)
        printf("\nPlease enter number from 1 up.");
    
        else
        {
         for(x=1; x<=inputNumber; ++x)
          {
           fact=(fact*x)/(2*inputNumber);
           sum+=fact;
          }
          printf("\nThe sum is %lf", sum);
        }
        getch();
    }
    Any suggestion to make this program better.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is my solution:
    A quick test shows that your solution is incorrect.

    Any suggestion to make this program better.
    For starters, change void main() to int main(), and remove your dependence on the non-standard <conio.h> by removing the unnecessary calls to clrscr() and getch().

    Now, for actually solving the problem, you can make use of the fact that for any integer n > 0, n!/(2n) = (n-1)!/2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    10
    I never know about this relation. Could you show me how to implement it in my code ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I never know about this relation.
    It is just simple arithmetic. n! = (n-1)! * n, therefore you can simplify as such. Actually, there is a further simplification, but that may actually complicate your code a slightly instead, so perhaps it is better to leave it for later.

    Could you show me how to implement it in my code ?
    First, implement a loop to sum from 1 to (n-1)! inclusive.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    I suggest creating a function that returns the factorial of a number.

    n! = (n - 1)! * n

    A recursive function might be:
    Code:
    int fact(int n)
    {
         if (n <= 1)
              return(1);
         return(fact(n - 1) * n);
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by noops View Post
    I suggest creating a function that returns the factorial of a number.

    n! = (n - 1)! * n

    A recursive function might be:
    Code:
    int fact(int n)
    {
         if (n <= 1)
              return(1);
         return(fact(n - 1) * n);
    }
    But it is often better to let the poster figure those things out him-/herself, as that is touching many more parts of the brain, and thus makes it easier to remember, v.s. selecting an area of text and pasting it into their editor.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not to mention that using such a function is inefficient.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    It's not efficient, but it's quite simple. I mean, if the OP has already problems getting a correct solution, we shouldn't ask about getting the most efficient solution, shouldn't we ?

    The only wrong thing I see with this function is the parameter and return type; I would definitely use an unsigned integer instead of a signed one, since the definition of factorial is only good for non-negative number.
    I hate real numbers.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    By writing the factorial as a seperate function I believe that the OP would not learn everything that they should be learning. They will get a solution, but will not have learned how to properly implement an algorithm that performs according the more desireable and easily achieveable O(n) Big-Oh notation.
    Last edited by iMalc; 08-01-2008 at 12:35 AM.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    10
    I found the mistake !
    In my code, just make change to the statement in the for loop like this:
    Code:
    for(x=1; x<=inputNumber; ++x)
          {
           fact=(fact*x)/(2*x);
           sum+=fact;
          }
    Tested and it works!

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Tested and it works!
    it is really strange - it should not
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Tested and it works!
    Sorry, it still does not work. For example, an input of 5 gives the result 0.96875, but clearly the answer is 17.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    10
    I don't know why it become like this, but when in typed in 3, it gives the result 2, which is the correct answer.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by chhay View Post
    I don't know why it become like this, but when in typed in 3, it gives the result 2, which is the correct answer.
    And do you also get the correct result for 4 or 5? It may be coincidence that 3 gives the correct result, despite the calculation itself being incorrect [and I agree that it most likely is].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by chhay View Post
    I don't know why it become like this, but when in typed in 3, it gives the result 2, which is the correct answer.
    In the words of Dijkstra: "Testing can only reveal the presence of bugs, not their absence". In other words, getting a correct answer for a single test doesn't mean that the whole program works.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Application Repeatition
    By lgcarter in forum C Programming
    Replies: 3
    Last Post: 06-16-2009, 02:07 PM
  2. Replies: 1
    Last Post: 05-28-2009, 01:28 PM
  3. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  4. a sum equal to or in excess of 100
    By lyoncourt in forum C Programming
    Replies: 6
    Last Post: 10-07-2007, 05:43 PM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 12:17 AM