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

• Comp Sci
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.

berkeman

FactChecker
Gold Member
It looks like your sqrt on line 8 includes more than you want it to.

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?

FactChecker
Gold Member
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.

berkeman
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.

FactChecker
Gold Member
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.

berkeman
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.

Mark44
Mentor
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;

FactChecker
FactChecker
Gold Member
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:
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.

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!

FactChecker
Gold Member
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.

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.

FactChecker
Gold Member
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.

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));
}

FactChecker
Gold Member
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.

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

FactChecker
Gold Member
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.

Mark44
Mentor
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.

FactChecker