A Tour of Go Exercise: Loops and Functions
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的更多相关文章
- A Tour of Go Exercise: Fibonacci closure
Let's have some fun with functions. Implement a fibonacci function that returns a function (a closur ...
- 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 ...
- [Training Video - 2] [Java Introduction] [Operator, Loops, Arrays, Functions]
Operator : While Loop : For Loop : Arrays : Code : public class FirstJavaClass { public static void ...
- A Tour of Go Exercise: Images
Remember the picture generator you wrote earlier? Let's write another one, but this time it will ret ...
- A Tour of Go Exercise: HTTP Handlers
Implement the following types and define ServeHTTP methods on them. Register them to handle specific ...
- A Tour of Go Exercise: Errors
Copy your Sqrt function from the earlier exercises and modify it to return an error value. Sqrt shou ...
- 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 ...
- exercise.tour.go google的go官方教程答案
/* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...
- C++ Core Guidelines
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
随机推荐
- httpcontext in asp.net unit test
[TestMethod] [HostType("ASP.NET")] [UrlToTest("http://localhost:25153/qq/a.aspx" ...
- BZOJ 4005 [JLOI 2015] 骗我呢
首先,我们可以得到:每一行的数都是互不相同的,所以每一行都会有且仅有一个在 $[0, m]$ 的数没有出现. 我们可以考虑设 $Dp[i][j]$ 为处理完倒数 $i$ 行,倒数第 $i$ 行缺的数字 ...
- android 监听去电实现ip拨号 广播接收者
利用广播实现ip拨号 布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- mac终端命令简介
mac终端命令简介(适合刚刚入手mac的新人们) 1.取得root权限 意义相当与windows中的超级管理员权限,甚至还要超出.root权限可以修改系统中的任何文件,不过对普通用户的意义不大,了解即 ...
- Django Aggregation聚合
在当今根据需求而不断调整而成的应用程序中,通常不仅需要能依常规的字段,如字母顺序或创建日期,来对项目进行排序,还需要按其他某种动态数据对项目进行排序.Djngo聚合就能满足这些要求. 以下面的Mode ...
- JavaScript中的自调用函数
自调用函数 自调用函数(self-invoking funciton)就是函数定义完之后会立即运行的函数. 最常见的写法是: (function() { // function body... }() ...
- [cocos2dx 3.x]Label类数字变化动作
之前写了个2.14版本的动作变化,见 http://www.cnblogs.com/creeper/p/3531304.html 3.x版本变化了很多,但是核心思想还是没有变化,所以对应3.x版本的改 ...
- Getting Started Tutorial
Getting Started Tutorial The topics contained in this section are intended to give you quick exposur ...
- BOM List demo
select level level_id, t.* from (select msi1.segment1 farther_item, msi1.inv ...
- css揭秘之linear-gradient
很神奇的背景设置, 看代码, <html> <title>css</title> <style> .content { background: line ...