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. php的post和get方法

    <?php function post($url,$fields) { $fields_string = ''; foreach($fields as $key=>$value) { $f ...

  2. centos7和windows7双系统安装

    前些天安装了双系统(centos7+win7),其实网上关于这类的教程很多,这篇日志也只是针对本人安装过程中遇到的一些问题进行说明.我是按照先安装win7再安装centos7的顺序. 1.关于分区: ...

  3. 卡牌手游源码《暗黑世界V1.3》数据库表说明文档!!!

    原地址:http://blog.csdn.net/uxqclm/article/details/11970761 欢迎来到9秒:www.9miao.com 由于看到论坛中有人询问需求<暗黑世界V ...

  4. unity 嵌入 百度分享 与 游戏内购物 iap

    原地址:http://blog.csdn.net/u012085988/article/details/18268869 最近老板让在unity项目里实现分享与内购功能,还要ios和android两个 ...

  5. spin_lock & mutex_lock的区别?

    http://blog.csdn.net/sunnytina/article/details/7615520   为什么需要内核锁? 多核处理器下,会存在多个进程处于内核态的情况,而在内核态下,进程是 ...

  6. 多核CPU怎么理解

    简而言之,双核处理器即是基于单个半导体的一个处理器上拥有两个一样功能的处理器核心.换句话说,将两个物理处理器核心整合入一个核中.企业IT管理者们也一直坚持寻求增进性能而不用提高实际硬件覆盖区的方法.多 ...

  7. make menuconfig出错

    make[1]: *** [scripts/kconfig/mconf] Error 1 make: *** [menuconfig] Error 2 fixed: sudo apt-get inst ...

  8. Linux 性能监测工具总结

    前言: Linux系统出现问题时,我们不仅需要查看系统日志信息,而且还要使用大量的性能监测工具来判断究竟是哪一部分(内存.CPU.硬盘……)出了问题.在Linux系统中,所有的运行参数保存在虚拟目录/ ...

  9. 转自 Good morning 的几句精辟的话

    1.志愿者招募 根据流量平衡方程来构图非常方便,而且简单易懂,以后可能成为做网络流的神法之一 简单记一下流量平衡方程构图法的步骤: a.列出需求不等式 b.通过设置松弛变量,将不等式变成等式 c.两两 ...

  10. Linux内核与根文件系统的关系

    开篇题外话:对于Linux初学者来说,这是一个很纠结的问题,但这也是一个很关键的问题!         一语破天机: “尽管内核是 Linux 的核心,但文件却是用户与操作系统交互所采用的主要工具.这 ...