Need suggestion on sum of factorials.

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-31-2008
chhay
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.
• 07-31-2008
laserlight
Quote:

This is my solution:
A quick test shows that your solution is incorrect.

Quote:

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.
• 07-31-2008
chhay
I never know about this relation. Could you show me how to implement it in my code ?
• 07-31-2008
laserlight
Quote:

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.

Quote:

Could you show me how to implement it in my code ?
First, implement a loop to sum from 1 to (n-1)! inclusive.
• 07-31-2008
noops
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); }```
• 07-31-2008
matsp
Quote:

Originally Posted by noops
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
• 07-31-2008
laserlight
Not to mention that using such a function is inefficient.
• 07-31-2008
foxman
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.
• 08-01-2008
iMalc
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.
• 08-01-2008
chhay
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!
• 08-01-2008
vart
Quote:

Tested and it works!
it is really strange - it should not
• 08-01-2008
laserlight
Quote:

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.
• 08-01-2008
chhay
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.
• 08-01-2008
matsp
Quote:

Originally Posted by chhay
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
• 08-01-2008
QuantumPete
Quote:

Originally Posted by chhay
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
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last