Go language Implementation Exercise--loops and Functions

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
To find the square root of a kind of called Newton algorithm of the text, first give a guess value, using the continuous approximation method to find the square root.
First we set the required number to a, its square root is x, and then we begin to make x=a, and then we enter a cycle, constantly making x= (x+a/x)/2, that is, X equals X and a/x mean, so that the iteration of about 7-10 times can get a square root x approximation.
Package Main


Import (
"FMT"
"Math"
)


Func Sqrt (x float64) float64 {
Z: = x
For I: = 0; I < 10; i++ {
z = (z + x/z)/2
}
Return Z
}


Func Main () {
Fmt. Println (SQRT (2))
Fmt. Println (Math. SQRT (2))
}

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.