As a simple way to play with functions and loops, implement the square root function using Newton's method.

In this case, Newton's method is to approximate Sqrt(x) by picking a starting point z and then repeating:

To begin with, just repeat that calculation 10 times and see how close you get to the answer for various values (1, 2, 3, ...).

Next, change the loop condition to stop once the value has stopped changing (or only changes by a very small delta). See if that's more or fewer iterations. How close are you to the math.Sqrt?

Hint: to declare and initialize a floating point value, give it floating point syntax or use a conversion:

z := float64(1)
z := 1.0
package main 

import (
"fmt"
) func Sqrt(x float64) float64{
var z float64 =
for i := ; i < ; i++ {
z = z - (z*z - x) / ( * z)
}
return z
}
func main() {
fmt.Println(Sqrt())
}

A Tour of Go Exercise: Loops and Functions的更多相关文章

  1. A Tour of Go Exercise: Fibonacci closure

    Let's have some fun with functions. Implement a fibonacci function that returns a function (a closur ...

  2. A Tour of Go Exercise: Slices

    Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit u ...

  3. [Training Video - 2] [Java Introduction] [Operator, Loops, Arrays, Functions]

    Operator : While Loop : For Loop :  Arrays : Code : public class FirstJavaClass { public static void ...

  4. A Tour of Go Exercise: Images

    Remember the picture generator you wrote earlier? Let's write another one, but this time it will ret ...

  5. A Tour of Go Exercise: HTTP Handlers

    Implement the following types and define ServeHTTP methods on them. Register them to handle specific ...

  6. A Tour of Go Exercise: Errors

    Copy your Sqrt function from the earlier exercises and modify it to return an error value. Sqrt shou ...

  7. A Tour of Go Exercise: Maps

    Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Tes ...

  8. exercise.tour.go google的go官方教程答案

    /* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...

  9. C++ Core Guidelines

    C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...

随机推荐

  1. 批量将MP4 转换为 MP3

    0 需要先下载VLC 软件 1 win+R 运行 "CMD" 2 CD mp4目录 3 复制 并运行下面代码 for %%a in (*.mp4) do "C:\Prog ...

  2. 【一起学OpenFOAM】04 OpenFOAM的学习资源

    OpenFOAM的学习资料并不多,个人猜测也许是与软件的类型有关系. 对于商用软件来讲,由于要占领市场,软件开发商自然是巴不得会用软件的人越多越好,因为他们卖的是软件,会用的人越多,软件卖得越好.他们 ...

  3. css3创建动画

    @keyframes ico{ 0% { top: -100%; } 100%{ top:4%; } } @-webkit-keyframes ico{ 0% { top: -100%; } 100% ...

  4. mysql导出数据库几种方法

    方法一 cmd 到mysql bin目录下用 如下命令 mysqldump --opt -h192.168.0.156 -uusername -ppassword --skip-lock-tables ...

  5. 9. MonoBehaviour.StartCoroutine 开始协同程序

    function StartCoroutine (routine : IEnumerator) : Coroutine 描述:开始协同程序. 一个协同程序在执行过程中,可以在任意位置使用yield语句 ...

  6. Ember.js demo6

    <!DOCTYPE html> <html> <head> <meta name="description" content=" ...

  7. 可否控制<link type=text/css rel=stylesheet href=style.css>

    本篇文章主要介绍了"可否控制<link type=text/css rel=stylesheet href=style.css> ", 主要涉及到可否控制<lin ...

  8. Sum of Digits / Digital Root

    Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...

  9. bzoj1492

    好题+神题,首先肯定是dp,我们设f[i]为到第i天能获得的最多的B卷(设获得的钱数亦可)由题目hint可知,要么全买要么全卖,我们有f[i]=max(maxmoney,f[j]*b[i]+f[j]* ...

  10. CodeForces_#354_Div.2_2016.5.25(A+B+C)

    A 描述:给出一串数,可以互换任意两个数的位置一次,求最大的数和最小的数的最大距离. 分析:找到最大的数和最小的数的位置,求右边的数到左端点的距离和左边的数到右端点的距离. #include < ...