A clever algorithm that uses C language to calculate the square root

Source: Internet
Author: User
A clever way to calculate the square root using C languageAlgorithm From: http://zhidao.baidu.com/question/186427911.html
 # include 
  
    void main () {int W, I; double ss; Double X, Y; printf ("Enter the number! \ N "); scanf (" % lf ", & Y);/* input floating point data y, that is, the equation we require the square root */x = y; /* assign y to X to save the value of Y first */SS = 1.0; For (W = 0; x> = 1; W ++) {x = X/10;}/* compress X to a decimal point, for example, 100.45 is changed to 0.10045 */W = W-1;/* and W is the X-digit minus one, for example, if you change 100.45 to 0.10045, then W = 2; */for (I = 1; I <= W; I ++) {Ss = SS * 10 ;} /* in the preceding example, the SS is changed to 100. So far, the previous work is to find the highest bit of this number. */X = 0; while (SS> 1.0/10e6) {for (; x * x 
   
     Y) {x = x-ss; SS = SS/10;} If (x * x = y) break;}/* the function of the preceding loop statement is as follows, take the preceding example: Set the square root to 0. * // * if its square is equal to the input value, it will jump out of the loop; otherwise, it will add 100. If it is greater than the input value, just cancel the action (x = x-ss;) and add 10 to it. If it is large, cancel it. If it is small, add, such a step-by-step loop */printf ("% lf \ n", x); getch () ;}
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.