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

In summary: Thanks!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.
  • #1
anonim
40
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 berkeman
Physics news on Phys.org
  • #2
It looks like your sqrt on line 8 includes more than you want it to.
 
  • #3
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?
 
  • #4
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 berkeman
  • #5
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.
 
  • #6
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 berkeman
  • #7
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.
 
  • #8
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 FactChecker
  • #9
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.
 

1. How does the recursive double code work?

The recursive double code works by repeatedly calling a function on itself, each time with a smaller input value. In this case, the function calculates the sum of the square roots of all numbers from 1 to the input number. It starts with the input number and recursively calls itself with half of the input number until it reaches 1.

2. What is the purpose of using recursion in this code?

The purpose of using recursion in this code is to break down a larger problem into smaller, more manageable subproblems. This allows for a more efficient and elegant solution, as well as making the code easier to understand and maintain.

3. Can you explain the base case in this recursive code?

The base case in this recursive code is when the input number is equal to 1. This is the stopping point for the recursive calls and allows the code to return the final sum of the square roots of all numbers from 1 to the original input number.

4. How does the recursive double code handle non-integer input numbers?

The recursive double code can handle non-integer input numbers by using a floating-point data type for the input and for the sum of the square roots. This allows for a more precise calculation of the sum, even for non-integer input numbers.

5. Can this code be optimized for better performance?

Yes, this code can be optimized for better performance by using memoization. This means that the code can store previously calculated values and retrieve them instead of recalculating them every time. This can greatly improve the speed and efficiency of the code, especially for larger input numbers.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
873
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
661
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
918
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
Back
Top