Recursion in C: Sum

Asked

Viewed 3,860 times

1

I’m learning Recursiveness in C and need to make a recursive function that returns the sum of a number n whichever. The prototype of the function is float somatorio(int n) and the sum to be calculated has the following formula: Σ (from i = 1 to i = n) {n / 1 + n}

So my question is this: the base case is 1? What about the sum calculation: How to define? I think it starts with:

float somatorio(int n){
  float i;
    if( n == 1){
            return n;
        }else{
            i = n;
            i = (n / 1 + n) + somatorio(); // o que viria dentro da segunda parte?   (1 + n)?
            return i;
        }
}

Definition of the exercise: Image address: https://uploaddeimagens.com.br/imagens/imagem-png--304

  • I don’t understand what this division by 1 is, it doesn’t make sense and why it has to add n´ com n`. What sum is this? I think this code should have 2 lines, maybe one. See if this is it: http://ideone.com/Hcvn1D

  • I then put an image with the program statement. Despite this, just below a user helped in the elaboration of the algorithm.

1 answer

4


Yes the base case is 1.
The base case is the simplest case and you know what the return value is for that case. This is the moment when your function returns a final element to the caller.
In your problem the known and simplest case is when n = 1, since all the sum in this case will end when n is equal to 1, in your problem we have that the sum of 1 is n/(1+n) => 1/(2) => 0.5. So say we want the sum of 5 that would be (5/6)+(4/5)+(3/4)+(2/3)+(1/2) correct?
we may also think that the sum of 5 is: 5/(1+5) + sum of 4. if we think that the sum of 4 is 4/(1+4) + the sum of 3 and so on then we would have to:
somatorio(5) = 5/(1+5) + somatorio(4)
somatorio(4) = 4/(1+4) + somatorio(3)
somatorio(3) = 3/(1+3) + somatorio(2)
somatorio(2) = 2/(1+2) + somatorio(1)
OPA! We know that the sum of 1 is 0.5 because it is our base case so we would have:
somatorio(2) = 2/(1+2) + 0.5 => 1.1666...
then we know that somatorio(2) = 1.1666, and now we can calculate the sum of 3, of 4 and finally of 5.

I wrote an example in code of the algorithm described above to try to demonstrate how it would look.

# include <stdio.h>
/*Função recursiva (calcula o somatorio de n)*/
float somatorio(int n)
{   
/*Veja que meu caso base é o somatório de 1
    e retorno o resultado que eu ja sei somatorio(1) = 1*/
    if (n == 1)
    {   
        return 0.5;
    }   
    /*Se não for meu caso base então eu 
    calculo o somatório do meu antecessor e somo o meu valor*/
    else 
    {   
        return ((float) n/(1+n) + somatorio(n-1));
    }   

}   

int main()
{   
    //Imprimo o resultado do somatorio de 5
    printf("%f", somatorio(5));
    return 0;
}   

I hope I’ve helped!

  • 1

    I think he meant \sum_{i=1}^n{ i \over 1 + i }, that would be return n / (n + 1.) + somatorio(n - 1);, somatorio() returning double. EDIT: The base case returns 0.5, also...

  • @Exact wtrmute. I checked that the Base case really is 0.5. As for what you suggested, that’s exactly it. So the function working correctly is this: float somatorio(int n){&#xA; if( n == 1){&#xA; return 0.5;&#xA; }else{&#xA; return n / (1. + n) + somatorio(n - 1);&#xA; }&#xA; }

  • @Wtrmute But I still have a doubt: Why in else{ return n / (1. + n) number one has to be accompanied by the .(decimal point)?

  • 1

    @Mike understood his problem now haha, but really the base case is still the sum of 1 and returns 0.5 in this case. As for the . of '1. 'It is necessary because in C a division of two integers returns an integer, when you place the point you are actually forcing 1 to be a real number and therefore the answer will be a real value. That is in C: 3/2 = 1 3/2. = 1.5 3. /2 = 1.5 3. /2. = 1.5 (float) 3/2 = 1.5 _In the latter case we are using what we know as cast, i.e., we are specifying the type of result that is expected by the operation.

  • 2

    @Mike: It’s like Wagjub mentioned; 1. (or 1.0, if you prefer) is a literal of type double, while 1 is a literal of type int (1.f or 1.0f is of the type float). We use this floating-point literal to force the result of the floating-point expression.

  • In fact, the interpretation of the answer below is not correct. The sum is defined by the formula. Therefore, yes, the sum of 1 is even 0.5, but the sum of 2, that is sum(2) is not 1.1666... , but 0.83, see: sum(2) = 1/1+1 + 1/1+2 = 1/2 + 1/3 = 5/6 or 0.8333. Thus sum(3) = 1/1+1 + 1/1+2 + 1/1+3 => 5/6 + 1/4 => 13/12 = 1,083333. We come to the induction step: sum(n) = sum(n-1) + 1/(n+1). Thus, the inductive formula is: sum(1) = 0,5; (base case) and sum(n) = sum(n-1)+1/(1+n) (induction step). Example link: https://repl.it/IpA5/6

  • @ed1rac in reality the interpretation of the problem was based on the formula presented by Mike that defines it as: somatorio is n/(n+1) and not 1/(n+1) as you interpreted.

  • He put the exercise definition in the image. Veja: Image address: https://uploaddeimagens.com.br/imagens/imagem-png-304

  • 1

    He really didn’t know much about explaining himself because he doesn’t have much knowledge of either recursiveness or summation. and the answer given is not correct! The problem is simple interpretation.

  • @But why isn’t it correct? In the code you propose in repl.it/Ipa5/6 you perform the sum with the constant numerator while in the image exercise the numerator is also variable. I entered my code in your link so that we could compare the differences because really "the problem is simple interpretation".

  • You’re right. Just change the numerator from one to n. I’ll fix it right away.

  • 2

    I’ve already changed the program. You’re absolutely right, @Wagjub To simplify the math I made the numerator one (for some inexplicable reason). Your code was correct. And so was what I did. I’ll give you a like in your reply. Sorry for the inconvenience! New link: https://repl.it/IpA5/9

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.