{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# r4 - RECURSION IV Finding square root of a number...

This preview shows pages 1–3. Sign up to view the full content.

RECURSION – IV Finding square root of a number Generating Permutations Additive sequence

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Finding Square Root of a number: We can use the Newton’s method, which updates a given guess. Let us take an example. Let the given number be 36. Let us guess that its root is 9. Now since 36/9 is 4, it is certain that the new value of the root has to lie between 9 and 4, as their product equals the given number. So the best way to make the next guess would be to choose a number lying somewhere in the middle of the range (4, 9). This would be ( 4 + 9 )/2, i.e. 6.5 which is more closer to the actual root 6 than the first guess 9 was. We can proceed further by computing the next factor 36/6.5 and then taking the mid value of this factor and 6.5. We see that we very rapidly approach 6, the true value of the root. The steps can be repeated till we find that the current step did not produce appreciable change in the value of the root. Thus given a number x, the first guess could be r = x/2. We keep on generating and testing successive approximations of r until we find the value that is close enough to stop. The updating process may be terminated when | r * r – x | < epsilon. A recursive solution to this problem would need the routine to be called repeatedly with the new value of the root. The stopping condition for the recursive function is already indicated above. #include <stdio.h> double sqroot(double num, double root); int main ( ) { double x,r; printf("\n enter x="); scanf("%lf", &x); r = x/2; r = sqroot(x,r); printf("\n root = %10.2f",r); printf("\n"); }
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern