Calculate the next sum recursive

Hello I have to calculate the next sum recursively :

S= 1! + 1/2! + 3! + 1/4! + 5! + 1/6! + ......+ 1/(2n)! + (2n+1)!
So I split that into 2 sums:
S1 = 1! + 3! + 5! + ..... + (2n+1)! ->> S(n - 1) + n!, if n is odd

S2 = 1/2! + 1/4! + 1/6! + ...... + 1/(2n)! ->> S(n - 1) + 1 / (n!), if n is even

And I have to choose the number, even or odd.
This is what I got so far :


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 #include <iostream>
using namespace std;

int factorial (int n)
{
	if (n==0) 
		return 1;
	else 
		return n*factorial(n-1);    //function for the factorial
}

float SumaPar (int n)
{
	if (n==0)
		return 0;
	else
		return SumaPar(n-1) + (1.0/factorial(n));   //function for the even numbers
}

float SumaImpar (int n)
{
	if (n==0)
         return 0;
	else
         return SumaImpar(n-1) + (factorial(n));  //function for the odd numbers
}

int main ()

{
int n;
float S=0;

cout<<"Introduceti n=";
cin>>n;

    {
    	if (n%2==0) 
    	{
    		S= (SumaImpar(n) + SumaPar(n)) - 1;
		}
		else
		{
		
		    S= (SumaImpar(n) + SumaPar(n-1)) - 1 ;
		}
	}
	
	cout<<"Suma este : "<<S;
	
        system("pause");
}


All works fine, but....instead of my sum it gives me this :

S=1!+2!+3!+4!+......+n! + 1/2! + 1/3! + 1/4! + 1/5! + ..... + 1/(2n+1)!

Any help will be apreciated.
Lets look at one term in your sum S1: k!.
What is the previous term? (k-2)!

How about in S2, where you have 1/k! ? 1/(k-2)!


In your code, however, your previous term is in SumaImpar(k-1)
Last edited on
The problem has to be resolved recursively...without for.That is the whole point of the exercise.

So?

what should I change ?
thedoctor2707 wrote:
So?

what should I change ?

Lets start with something else. I took bits of your code and made it to print the terms that you use in your calculations:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using std::cout;

const int STEP = 1;

void SumaPar (int n) {
	if (n<=0)
	  return;
	else {
      cout << " + 1/" << n << '!';
	  SumaPar( n - STEP );
	}
}

void SumaImpar (int n) {
	if (n<=0)
      return;
	else {
      cout << " + " << n << '!';
      SumaImpar( n - STEP );
	}
}

void serie( int n ) {
  if (n%2==0) {
    SumaImpar( n );
    SumaPar( n );
  }
  else {
    SumaImpar( n );
    SumaPar( n - 1 );
  }
  cout << " - 1\n";
}

int main ()
{
  cout << "For odd (3):\n";
  serie( 3 );
  cout << "For even (4):\n";
  serie( 4 );

  return 0;
}

For odd (3):
 + 3! + 2! + 1! + 1/2! + 1/1! - 1
For even (4):
 + 4! + 3! + 2! + 1! + 1/4! + 1/3! + 1/2! + 1/1! - 1

First, your largest term is n, rather than (2n+1).

Second, you use every consecutive value on both sides, rather than using every second value. This problem you did notice. I did add constant STEP as a hint.

Third, the terminating condition in the recursions. I did change that, even though your version works with STEP==1. Can you deduce why?
If you make them even smarter, then you will need a bit less code elsewhere.

Four, your even vs odd input treatment (within your main) is not symmetric. On one branch you do add -1 to one function call, but not on the other. You don't even need the branching, because 2n is always even and 2n+1 is always odd.
Last edited on
Topic archived. No new replies allowed.