Home > database >  Simplifying a Recursive Function
Simplifying a Recursive Function

Time:11-03

I have a recursive function that sums all even numbers within an integer range inclusively. The code I have right now works properly, but it looks like it can be simplified. I've tried putting the recursive function in different spots, but every time I get the wrong answer. Can you guys show me how I can shorten my code?

int sum_evens(int range_start, int range_end)
{
   int even_sum = 0; /* The inclusive sum of all even numbers within  */
                     /* whole number range                            */

   printf("\n   Entering sum function for range %d to %d", 
                                            range_start, range_end);
                                            
   if(range_start <= range_end)
   {
      if(is_even(range_start) == 0)
      {
         printf("\n      Adding: %d", range_start);
         even_sum = sum_evens(range_start   1, range_end);
         even_sum  = range_start;
      }
      else
      {
         printf("\n      Skipping: %d", range_start);
         even_sum = sum_evens(range_start   1, range_end);
      } 
  }

  printf("\n   Exiting sum function for range %d to %d with result: %d",
     range_start, range_end, even_sum);
  
 return even_sum;

}

I'm basically trying to only have the line (even_sum = sum_evens(range_start 1, range_end);)

CodePudding user response:

Well, there is a math formula for calculating this but if you really need recursion, you can try:

int sum_evens(int range_start, int range_end)
{
    if (range_start > range_end) return 0;
    int tmp = (is_even(range_start) == 0) ? range_start : 0;
    return tmp   sum_evens(range_start   1, range_end);
}

But notice that such a recursive function is "dangerous". Calling it like sum_evens(0, 2000000000) is very likely to generate a stack overflow.

So if you don't want to use the math formula, a much better approach would be a simple loop.

int sum_evens(int range_start, int range_end) {
    int sum = 0;
    if (is_even(range_start) != 0)   range_start; // Make range_start even
    while(range_start <= range_end) 
    {
        sum  = range_start;
        range_start  = 2;
    }
    return sum;
}

BTW:

Your function is_even apparently return 0 when the number is even. That's rather unusual as the value 0 typically means false. However, in the above I kept the same style.

CodePudding user response:

I understand you want to do this recursively, but just like what the comments says, if you're looking for a better and faster solution you could use a simple iteration or the actual arithmetic formula, that being said

#include <stdio.h>

int sum_evens(int range_start, int range_end) {
    printf("Entering sum function for range %d to %d\n",
           range_start, range_end);

    if (range_start > range_end) return 0;
    int val = range_start % 2 == 0 ? range_start : 0;
    return val   sum_evens(range_start   1, range_end);
}

int main() {
    int val = sum_evens(1, 10);
    printf("\n%d", val);
    return 0;
}

here instead of using an if else statement to add values to the total sum, you can use the ternary operator, and then call the function again to initiate a recursion

If you want to use a void function and pointers

#include <stdio.h>

void sum_evens(int range_start, int range_end, int * initiator) {

    printf("Entering sum function for range %d to %d\n", range_start, range_end);

    if (range_start <= range_end) {
        int val = range_start % 2 == 0 ? range_start : 0;
        (*initiator)  = val;
        sum_evens(range_start   1, range_end, initiator);
    }
}

int main() {
    int sum = 0;
    sum_evens(1, 3, &sum);
    printf("\n%d", sum);
    return 0;
}

Using iteration (for loop)

#include <stdio.h>

int sum_evens(int range_start, int range_end) {
    int sum = 0;
    for (int i = range_start; i <= range_end; i  ) 
        if (i % 2 == 0) sum  = i;
    return sum;
}

int main() {
    int val = sum_evens(1, 10);
    printf("\n%d", val);
    return 0;
}

CodePudding user response:

Nothing more to add but have a habit of writing base condtion at first so to decrease execution time and space it will allocate, as in here your function went up to n 1 also which is uneccesary.

  • Related