Square root of X

Source: Internet
Author: User

Method 1:2 points to find

x = 24epsilon = 0.01numGuesses = 0low = 0.0high = Max (1.0,x) ans = (high + low)/2.0while abs (ans*ans-x) >=epsilon:    print ' Low: ', Low, ' High: ', high, ' ans = ', ans    numguesses + = 1    if ans**2 <x: Low        = ans    else: high        = Ans    ans = (high + low)/2.0print ' numguesses = ', numguessesprint ans, ' was close to square root of ', X


low:0.0 high:24 ans = 12.0low:0.0 high:12.0 ans = 6.0low:0.0 high:6.0 ans = 3.0low:3.0 high:6.0 ans = 4.5low:4.5 high:6.0 ans = 5.25low:4.5 high:5.25 ans = 4.875low:4.875 high:5.25 ans = 5.0625low:4.875 high:5.0625 ans = 4.96875 low:4.875 high:4.96875 ans = 4.921875numGuesses = 94.8984375 is close to square root of 24


Method Two: Isaac Newton method

Epsilon = 0.01k = 24.0guess = K/2.0numguesses = 0while ABS (guess*guess-k) >=epsilon:    guess = guess-((guess**2-k )/(2*guess))    numguesses + = 1print ' numguess = ', numguessesprint ' Square root of ', K, ' is about ', guess

Numguess = 4Square root of 24.0 is about 4.89898874321


Square root of X

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.