# 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. 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.

2. I think it could be the data type problem.
Any idea about data type ?

3. Originally Posted by chhay
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

4. 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)!.

5. Becasue when I follow the loop and calculate it on a paper it gives the correct result. Input 5 will get 17.

6. 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.

7. The sum of factorials part is correct. When i input 5, the result is 153, this sum of factorials is correct.

8. 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?

9. 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. 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);```

11. 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. 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.

13. Thanks, i will try using the (N-1)!/2 idea later.

Page 2 of 2 First 12
Popular pages Recent additions