Recursive Double code to Calculate the sum of the square roots <= a number

Click For Summary

Discussion Overview

The discussion revolves around a coding problem related to calculating the sum of square roots recursively in C programming. Participants explore various implementations, identify issues in the provided code, and discuss the appropriateness of recursion for different calculations, including standard deviation.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant presents a recursive function intended to calculate the sum of square roots but notes that the output is incorrect.
  • Some participants suggest that the expression inside the square root function is incorrect and needs adjustment.
  • Hints are provided regarding the structure of the recursive function, emphasizing the need to ensure only the intended value is passed to the square root function.
  • A later reply proposes an alternative approach to calculate the sum of integers down to one, suggesting that recursion may not be suitable for all problems.
  • Another participant expresses a desire to calculate the standard deviation recursively, prompting a discussion about the limitations of recursion for this calculation.
  • Concerns are raised about the changing value of the variable 'n' in the recursive function, which complicates the calculation.
  • Some participants argue that a for-loop would be a simpler solution for the standard deviation calculation, contrasting it with the recursive approach.

Areas of Agreement / Disagreement

Participants generally agree that the original recursive approach has issues and that recursion may not be the best method for certain calculations, such as standard deviation. However, there is no consensus on the best approach to implement the desired calculations.

Contextual Notes

Participants note that recursion works well for problems that can be broken down into simpler, repetitive tasks, but not all problems are suited for this method. The discussion highlights the importance of understanding the structure of the problem when choosing between recursion and iterative solutions.

anonim
Messages
39
Reaction score
2
Homework Statement
Calculate the sum of the square root
Relevant Equations
-
C:
#include<stdio.h>
#include<math.h>
double foo(int n){
    if(n==1){
        return(1);
    }
    if(n!=0){
        return( sqrt((n)+foo(n-1) ) );
    }
}
int main(){
    int num;
    printf("Enter the number: ");
    scanf("%d",&num);
    foo(num);
    printf(" %lf ",foo(num));
   return(0);
}
I want to calculate the sum of the square root. For example if the input is 5, I want to calculate sqrt(5)+sqrt(4)+sqrt(3)+sqrt(2)+sqrt(1) recursively. But it does not work. If the input is 5, output -> 2.735877.
 
  • Like
Likes   Reactions: berkeman
Physics news on Phys.org
It looks like your sqrt on line 8 includes more than you want it to.
 
FactChecker said:
It looks like your sqrt on line 8 includes more than you want it to.
I think so, do you have any idea how I can fix this?
 
Since this is a homework problem, I can only give hints. Make sure that what you have inside the parentheses of sqrt() is exactly what you want to take the sqrt of.
 
  • Like
Likes   Reactions: berkeman
FactChecker said:
Since this is a homework problem, I can only give hints. Make sure that what you have inside the parentheses of sqrt() is exactly what you want to take the sqrt of.
I tried a lot but it does not happen.. :( Anyway thank you for help.
 
anonim said:
I tried a lot but it does not happen.. :( Anyway thank you for help.
Don't give up now. You are 99% done. Do you want sqrt(5)+sqrt(4)+... or sqrt( 5 + sqrt(4 + sqrt(3 + ...))))? Check that line. Your code now is calculating the second option.
 
  • Like
Likes   Reactions: berkeman
FactChecker said:
Don't give up now. You are 99% done. Do you want sqrt(5)+sqrt(4)+... or sqrt( 5 + sqrt(4 + sqrt(3 + ...))))? Check that line. Your code now is calculating the second option.
Code:
return( sqrt((n))+foo(n-1)  );
I write like this and it works. Now I'm wondering where I would write the (n) if I wanted to calculate it sqrt((5+4+3+2+1)/n) recursively.
 
anonim said:
Code:
return( sqrt((n))+foo(n-1)  );
I write like this and it works.
You have more parentheses than you need. This would also work.
C:
return( sqrt(n)+foo(n-1)  );
Or even this:
C:
return sqrt(n)+foo(n-1) ;

The idea is that all of these do the same thing:
C:
return ((x) + (y));
return (x + y);
return x + y;
 
  • Like
Likes   Reactions: FactChecker
anonim said:
Now I'm wondering where I would write the (n) if I wanted to calculate it sqrt((5+4+3+2+1)/n) recursively.
This is not well suited for a solution by recursion. (and what value does the denominator, n, have?). The only part that would work with recursion is the summation 5+4+3+2+1 :
C:
int sumDownToOne(int n){
    if(n==1){
        return(1);
    }
    if(n!=0){
        return( n+sumDownToOne(n-1) );
    }
}
And that is hardly worth it.

You should be aware that there are only certain things that recursion works well on. Recursion works well when a process can be done repetitively by applying the same process on a little simpler problem until it gets to the last repetition that is so simple that it can be done directly.
One of the best examples of an elegant solution using recursion is the Towers of Hanoi Problem. It is not easy to solve any other way, but is very simple to solve using recursion. See solving Towers of Hanoi using recursion.
 
Last edited:
  • #10
FactChecker said:
This is not well suited for a solution by recursion. (and what value does the denominator, n, have?). The only part that would work with recursion is the summation 5+4+3+2+1 :
C:
int sumDownToOne(int n){
    if(n==1){
        return(1);
    }
    if(n!=0){
        return( n+sumDownToOne(n-1) );
    }
}
And that is hardly worth it.

You should be aware that there are only certain things that recursion works well on. Recursion works well when a process can be done repetitively by applying the same process on a little simpler problem until it gets to the last repetition that is so simple that it can be done directly.
One of the best examples of an elegant solution using recursion is the Towers of Hanoi Problem. It is not easy to solve any other way, but is very simple to solve using recursion. See solving Towers of Hanoi using recursion.
not n, n-1. I typed wrong. Forget it and let's say I want to calculate standard deviation recursively.
 
  • #11
Mark44 said:
You have more parentheses than you need. This would also work.
C:
return( sqrt(n)+foo(n-1)  );
Or even this:
C:
return sqrt(n)+foo(n-1) ;

The idea is that all of these do the same thing:
C:
return ((x) + (y));
return (x + y);
return x + y;
Thanks!
 
  • #12
anonim said:
not n, n-1.
That still has the same problem since I don't know if n runs through a series of numbers or has a single, fixed, value.
I typed wrong. Forget it and let's say I want to calculate standard deviation recursively.
Recursion would not be a good way to calculate the Standard Deviation (SD). Are you aware of the formula for SD that only uses ##\sum {{x_i}^2}## and ##(\sum {x_i})^2##? That allows one to run through the data only once and calculate the SD after that.
 
  • #13
FactChecker said:
That still has the same problem since I don't know if n runs through a series of numbers or has a single, fixed, value.
Recursion would not be a good way to calculate the Standard Deviation (SD). Are you aware of the formula for SD that only uses ##\sum {{x_i}^2}## and ##(\sum {x_i})^2##? That allows one to run through the data only once and calculate the SD after that.
C:
#include<stdio.h>
#include<math.h>
double foo(int n, double mean){
    double square;
    if(n==1){
        return(1);
    }
    if(n!=0){
      
        return( (sqrt((n-mean)*(n-mean))+foo(n-1,mean))/(sqrt(n-1))  );
    }}
int main(){
    int num;
    double mean;
    int i;
    int sum=0;
    printf("Enter the number: ");
    scanf("%d",&num);
    for(i=1; num>=i; i++){
        sum=sum+i;
    }
    mean=(double)sum/num;
    printf("%lf ",mean);
    foo(num,mean);

    printf(" %lf ",foo(num,mean));
}

I tried to write like this. But it does not work. https://i.pinimg.com/originals/b5/7c/a0/b57ca00c2aabd05bcb722295734ba2e6.png I tried the apply the formula in the link.
 
  • #14
anonim said:
I tried to write like this. But it does not work. https://i.pinimg.com/originals/b5/7c/a0/b57ca00c2aabd05bcb722295734ba2e6.png I tried the apply the formula in the link.
That is not calculating the same thing as the formula in the link. Notice that the n in the link has only one, fixed, value. It is the total number of data points. Your code keeps changing the value of n. In line 10, you have "foo(n-1,mean)", so it is passing in n-1, which keeps being reduced for each lower-level call.
I think that you are trying to use recursion where it just makes things more complicated.
 
  • #15
FactChecker said:
That is not calculating the same thing as the formula in the link. Notice that the n in the link has only one, fixed, value. It is the total number of data points. Your code keeps changing the value of n. In line 10, you have "foo(n-1,mean)", so it is passing in n-1, which keeps being reduced for each lower-level call.
I think that you are trying to use recursion where it just makes things more complicated.
at first I added another value that holds the number in the function like this;
Code:
#include<stdio.h>
#include<math.h>
double foo(int n, double mean, int n2){
    if(n==1){
        return(1);
    }
    if(n!=0){
     
        return( (sqrt((n-mean)*(n-mean))+foo(n-1,mean,n2))/(sqrt(n2-1))  );
    }}
int main(){
    int num;
    int n2;
    double mean;
    int i;
    int sum=0;

    printf("Enter the number: ");
    scanf("%d",&num);
    n2=num;
    for(i=1; num>=i; i++){
        sum=sum+i;
    }
    mean=(double)sum/num;
    printf("%lf ",mean);
    foo(num,mean,n2);

    printf(" %lf ",foo(num,mean,n2));
}
 
  • #16
You are trying to use recursion where it just makes things harder. It is much easier to just use a for-loop to make the correct calculation. To see an example where recursion really simplifies things, see the link I gave in post #9 to the Towers of Hanoi problem.
 
  • #17
FactChecker said:
You are trying to use recursion where it just makes things harder. It is much easier to just use a for-loop to make the correct calculation. To see an example where recursion really simplifies things, see the link I gave in post #9 to the Towers of Hanoi problem.
Yes I can use loop but my teacher does not allow loop in this homework
 
  • #18
anonim said:
Yes I can use loop but my teacher does not allow loop in this homework
Did you teacher pick the example of standard deviation for recursion or is this your example? IMHO, it is a very bad example for recursion.
I would not use recursion in this example for anything except doing the summation, using code like I put in post #9. I would keep the sqrt() and the division by n out of the recursion and only do that after the recursive summation.
 
  • #19
FactChecker said:
Did you teacher pick the example of standard deviation for recursion or is this your example? IMHO, it is a very bad example for recursion.
From what I can tell, the assignment is something like this:
"Write a program in C that uses recursion."
The choice to implement a function that calculates the standard deviation or the sum of square roots or whatever seems to me the idea of the OP here.

@anonim, you could write a recursive program that calculates this:
$$\frac 1 N (\sqrt 1 + \sqrt 2 + \sqrt 3 + \dots + \sqrt N)$$

but this has nothing to do with standard deviation.

BTW, homework questions should be posted in this section, not in the Computer Science general section. I deleted your post there that seems to be a continuation of what you have started here.
 
  • #20
Another standard example of recursion is the factorial. Although it is not an example where the factorial is better than a simple loop, it is a simple example to program.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 80 ·
3
Replies
80
Views
10K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K