Need suggestion on sum of factorials.

This is a discussion on Need suggestion on sum of factorials. within the C Programming forums, part of the General Programming Boards category; I don't know why it become like this, but when in typed in 3, it gives the result 2, which ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    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.
    With your updated code, an input of 3 gives a sum of 0.875. It is your original code that gives a sum of 2 for an input of 3. In any case, a single incorrect result is sufficient to prove that your implementation is incorrect.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Jul 2008
    Posts
    10
    I think it could be the data type problem.
    Any idea about data type ?

  3. #18
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by chhay View Post
    I think it could be the data type problem.
    Any idea about data type ?
    When your numbers don't come out right, it's the algorithm. Not the data type. Don't blame C for getting incorrect results!

    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

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    I think it could be the data type problem.
    Any idea about data type ?
    I have written a version that uses unsigned long, but as it is the data types you chose can be used.

    You are on the right track, but the main problem seems to be that you have not figured out how to correctly sum the factorials. Pretend that there is no divisor, and just compute the sum of the factorials. In other words, write a program to compute the sum of 1! + 2! + ... + N!. Actually, as I have pointed out, you only need to sum until (N-1)!.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Jul 2008
    Posts
    10
    Becasue when I follow the loop and calculate it on a paper it gives the correct result. Input 5 will get 17.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Becasue when I follow the loop and calulate it on a paper it gives the correct result. Input 5 will get 17.
    As I said, you are on the right track. In fact, you are very close... so it is likely that the algorithm you followed on paper differs slightly from the algorithm you implemented. This is a matter of the divisor being in the wrong place, which is why I suggested that you backtracked a little to get the sum of factorials part done correctly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Jul 2008
    Posts
    10
    The sum of factorials part is correct. When i input 5, the result is 153, this sum of factorials is correct.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    The sum of factorials part is correct. When i input 5, the result is 153, this sum of factorials is correct.
    That's great. So what was the code that you used?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Jul 2008
    Posts
    10
    Here the code:
    Code:
    for(x=1; x<=inputNumber; ++x)
          {
           fact=fact*x;
           sum+=fact;
          }
    It find the sum of factorials. But if i add the dividen part like the following it doesn't give the correct answer.

    Code:
    for(x=1; x<=inputNumber; ++x)
          {
           fact=(fact*x)/(2*x);
           sum+=fact;
          }
    Notice variable fact and x have different data type.

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    But if i add the dividen part like the following it doesn't give the correct answer.
    Now, look at where you added the divisor. By writing "fact=(fact*x)/(2*x)", you are saying that a factorial is computed with the divisor. But clearly, the factorial is not computed with the divisor. Rather, the sum is computed by adding the factorial divided by the divisor. Therefore, you should write:
    Code:
    fact=fact*x;
    sum+=fact/(2*x);
    Or more succinctly:
    Code:
    fact *= x;
    sum += fact / (2 * x);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Jul 2008
    Posts
    10

    Lightbulb

    Thanks, laserlight.

    I didn't keep my eyes on the second line in the loop. I'm so stupid.

    It's working correctly at the moment. Thank you very much. You are the best. Now i can focus on my Maths exam on the next day.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    It's working correctly at the moment. Thank you very much. Now i can focus on my Maths exam on the next day.
    When you have the time, you might want to look into using the (N-1)!/2 idea. It allows you to move the division out of the loop (so you only divide once) and reduces the number of iterations by 1.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Registered User
    Join Date
    Jul 2008
    Posts
    10
    Thanks, i will try using the (N-1)!/2 idea later.

Page 2 of 2 FirstFirst 12
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, 07: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

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