Help me Out With Factorials

How to write a program for the sum of this series

Sum= 1/1! + 2/2! + 3/3! + 4/4! + 5/5! + 6/6!
what have you got so far? You only need to use a loop and write a function to calculate factorial.
Last edited on
You don't need a separate function for factorial. It is easily calculated as part of the sequence. (You only need to know what the last term in the sequence was to compute the current.)

It always helps to write things out:

       1     2      3        4
sum = --- + --- + ----- + ------- + ...
       1    1*2   1*2*3   1*2*3*4

See any patterns?
Also, is there anything that can be reused from one term to the next?

Hope this helps.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

int main()
{
	double sum = 0;
	int dProduct = 1;

	for(int i = 1; i <= 6; i++)
	{
		dProduct *= i;

		sum += i / static_cast<double>(dProduct);
	}

	std::cout << sum;
}


Hope this helps!

EDIT
So the formula for this is basically y = x/ x!. This is saying that as x approaches infinity, y will get infinitely closer to zero. That is why at around 2.718281 it stops incrementing by a notable amount.

EDIT 2
The limit is e. I just noticed that.
Last edited on
Topic archived. No new replies allowed.