Series Function

Write a function that returns the sum of the series 1! + 2! + 3! + 4! + ... + n! where n is an integer passed to the function as an argument.

This is what I have come up with but it will not compile. What am I doing wrong?
Thank you!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cmath>


using namespace std;

float Series( float fTerms)
{
	double dResult = 0;
	for(int i = 1; i <= fTerms; i++;)
	
	dResult = dResult + fTerms;
	
	return dResult;
}

int main (void)
{
	int iTerms = 0;
	float fResult = 0.0;
 	cout << "Please Enter the number of terms" << endl;
 	cin >> iTerms; 
 	
 	fResult = Series(iTerms);
 	
 	cout << "THE SUM IS: " << fResult << endl;
 	 	
 	
 	return 1;
}
Last edited on
1
2
3
4
5
6
7
8
9
float Series( float fTerms)
{
	double dResult = 0;
	for(int i = 1; i <= fTerms; i++;) //<--- The last semicolon doesn't need to be there.
	
	dResult = dResult + fTerms;
	
	return dResult;
}


Listen to your compiler. It would have told you there was a syntax error, and exactly what line to find it on.
Thank you

I went ahead and corrected the syntax error and the code compiled. When I tested the code it is not giving me the sum of the series but actually just squaring the input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cmath>


using namespace std;

float Series( float fTerms)
{
	double dResult = 0;
	for(int i = 1; i <= fTerms; i++)
	
	dResult = dResult + fTerms;
	
	return dResult;
}

int main (void)
{
	int iTerms = 0;
	float fResult = 0.0;
 	cout << "Please Enter the number of terms" << endl;
 	cin >> iTerms; 
 	
 	fResult = Series(iTerms);
 	
 	cout << "THE SUM IS: " << fResult << endl;
 	 	
 	
 	return 1;
}
Where are you doing the math for the factorials? I don't see anywhere in your code where you tried to do that.

1
2
3
4
5
6
7
8
9
10
float Series( float fTerms)
{
	double dResult = 0;
	for(int i = 1; i <= fTerms; i++) //this will loop fterm times.
	    dResult = dResult + fTerms;  //you add fterm + fterm + fterm ... (fterm times)
            //which is the same thing as saying fterm*fterm. 
            //You're not actually computing anything about that series here.
	
	return dResult;
}
I managed to do the factorials. My issue is summing the factorial of each term.
1! + 2! + 3! + 4! + ... + n!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cmath>


using namespace std;

float Series( float fTerms)
{
	double dResult = 0;
	for(int i = 1; i <= fTerms; i++)
	
	dResult = ((fTerms-1)*fTerms);
	
	return dResult;
}

int main (void)
{
	int iTerms = 0;
	float fResult = 0.0;
 	cout << "Please Enter the number of terms" << endl;
 	cin >> iTerms; 
 	
 	fResult = Series(iTerms);
 	
 	cout << "THE SUM IS: " << fResult << endl;
 	 	
 	
 	return 1;
}
Perhaps you could make a function to do the factorial of a term?
float Factorial(float term)

1
2
for(int i = 1; i <= fTerms; i++)
    dResult += Factorial(i);
Last edited on
I would do the factorial and sum in one loop to save function calls
1
2
3
4
5
6
long double reult = 0, factorial = 1;
for(int i = 0; i < n; ++i)
{
    factorial *= i;
    result += factorial;
}

That works also. But making a function for factorial would be a great idea too. There's no performance waste in creating functions. In fact, having more functions is generally good thing. I'm sure somewhere down the line you'll want to find the factorial of something again, then you already have a function written for that.
It's generally good practice to make reusable functions wherever you can. You can't really go wrong with it.
@Thumper
In this case a function call would be a waste. The call to factorial() would need to occur every iteration basically giving it polynomial time complexity. Also, IMO calculating factorial is so trivial code reuse isn't really a big deal (in this specific situation).

My previous code has some typos, this is working for n=5
1
2
3
4
5
6
7
long long result = 0, factorial = 1;
    int n = 5;
    for(int i = 1; i <= n; ++i)
    {
        factorial *= i;
        result += factorial;
    }

edit:Not sure why I was using long double, but changed to long long.
Last edited on
> The call to factorial() would need to occur every iteration basically giving it polynomial time complexity
Implementation dependent. You could use memorization.

Still, considering how lost is the OP, an aux function would help a lot.
Still, considering how lost is the OP, an aux function would help a lot.
Good point.
@naraku9333
I'm sorry. For the sake of learning, it's good practice to make reusable functions wherever you can.
Even so, multiple function calls to factorial() (because it's such a simple function) will have such a minuscule affect on process time/resources, especially considering today's technology, that you wouldn't be able to tell a difference.
That said, you're correct. You have given the most efficient solution to the problem.
Topic archived. No new replies allowed.