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 ...
随机推荐
- 仿今日头条最强顶部导航指示器,支持6种模式-b
项目中经常会用到类似今日头条中顶部的导航指示器,我也经常用一个类似的库PagerSlidingTabStrip,但是有时并不能小伙伴们的所有需求,所以我在这个类的基础上就所有能用到的情况做了一个简单的 ...
- python 删除文件和文件夹
1.删除文件 '''删除文件 ''' def DeleteFile(strFileName): fileName = unicode(strFileName, "utf8") if ...
- 简单的map转换成Bean的工具
简单的map转换成Bean的工具 package com.sd.microMsg.util; import java.lang.reflect.Field; import java.lang.refl ...
- POj 2186 Popular Cows[连通分量]
题目大意:给出N头牛,有M种关系u, v.代表u牛崇拜v牛.要求找出有多少头牛被所有牛崇拜着题目链接:http://poj.org/problem?id=2186解题思路:1>求出强连通分量,标 ...
- 如何确定照片是否被PS过
除了用软件,还可以先右键属性----解除锁定----重新打开属性看详细信息.
- hdu 3717
思路:二分答案,然后模拟消灭石头的过程: 如果单纯的暴力模拟的话,肯定会T的: 所以要用到一定的技巧来维护: 在网上看到大神们用O(n)的复杂度来优化,真心orz: 原理是这样的:用一个变量sum_2 ...
- zepto源码学习-02 工具方法-详细解读
上一篇:地址 先解决上次留下的疑问,开始看到zepto.z[0]这个东西的时候,我很是不爽,看着它都不顺眼,怎么一个zepto的实例对象var test1=$('#items'); test__pr ...
- java super 隐式参数
第41集 所有构造器里,第一句话就是super() (隐式的,系统自动执行) 鸟构造器调用动物构造器,动物构造器调用object构造器. (系统默认的) tostring() 方法 ...
- spring-boot启动debug信息中non-fatal error解决
java.lang.ClassNotFoundException: org.springframework.data.web.config.EnableSpringDataWebSupport添加依赖 ...
- Android开发UI之常用控件的使用
1.日期选择控件 DatePickerDialog 代码: btnChooseDate=(Button) findViewById(R.id.btnChooseDate); btnChooseDate ...