Skip to content

Instantly share code, notes, and snippets.

@tetsuok
Created April 2, 2012 01:58
Show Gist options
  • Select an option

  • Save tetsuok/2279991 to your computer and use it in GitHub Desktop.

Select an option

Save tetsuok/2279991 to your computer and use it in GitHub Desktop.
An answer of the exercise: Loops and Functions on a tour of Go
package main
import (
"fmt"
"math"
)
const Delta = 0.0001
func isConverged(d float64) bool {
if d < 0.0 {
d = -d
}
if d < Delta {
return true
}
return false
}
func Sqrt(x float64) float64 {
z := 1.0
tmp := 0.0
for {
tmp = z - (z * z - x) / 2 * z
if d := tmp - z; isConverged(d) {
return tmp
}
z = tmp
}
return z
}
func main() {
attempt := Sqrt(2)
expected := math.Sqrt(2)
fmt.Printf("attempt = %g (expected = %g) error = %g\n",
attempt, expected, attempt - expected)
}
@ceaksan
Copy link
Copy Markdown

ceaksan commented Feb 28, 2018

package main

import (
	"fmt"
	"math"
)

func Sqrt(x float64) float64 {
	z := float64(1)
	for i := 0; i < 10; i++ {
		z = z - (z * z-x) / (2 * z)
	}
	return z
}

func main() {
	i := float64(169)
	fmt.Println(Sqrt(i), Sqrt(i) == math.Sqrt(i))
}

@danong
Copy link
Copy Markdown

danong commented Mar 28, 2018


import (
	"fmt"
	"math"
)

func Sqrt(x float64) float64 {
	z := x / 2
	for prev := 0.0; math.Abs(prev-z) > 1e-8; {
		prev = z
		z -= (z*z - x) / (2 * z)
	}
	return z
}

func main() {
	fmt.Println(Sqrt(8))
}

@DersioK
Copy link
Copy Markdown

DersioK commented Jul 9, 2018

Simplest way to implement the code is as follows:

import "fmt"

func Sqrt(x float64) float64 {
z := float64(1)
for i:=0;i<10;i++{
z -= (zz - x) / (2z)
fmt.Println(z) //Just to view each iteration of z
}
return z
}

func main() {
fmt.Println(Sqrt(2))
}

@justinpage
Copy link
Copy Markdown

justinpage commented Jul 22, 2018

Here's another take. Instead of fooling around with the delta and absolute values, let's convert the type to something more finite.

package main

import (
	"fmt"
	"math"
)

func Sqrt(start float64) float64 {
	for guess := 1.0; ; {
		state := guess
		guess -= (guess*guess - start) / (2 * guess)

		if float32(state) == float32(guess) {
			return guess
		}
	}
}

func main() {
	fmt.Println(Sqrt(6.0) == math.Sqrt(6.0)) // true
	fmt.Println(Sqrt(2) == math.Sqrt(2))     // true
	fmt.Println(Sqrt(866) == math.Sqrt(866)) // true
}

@TomYeoman
Copy link
Copy Markdown

// SquareRoot x
func SquareRoot(x float64) float64 {
	z, count, thresh := float64(1), 10, float64(0.1)

	for index := 0; index < count; index++ {
		z = z - ((z*z - x) / (2 * z))

		fmt.Printf("Z Val [ %v ] , Predicted [ %v ], Actual [ %v ] \n", z, z*z, x)

		if guess := z * z; math.Abs(guess-x) < thresh {
			fmt.Printf("Fell within threshold of [ %v ] within [ %v ] counts \n", thresh, index)
			break
		}
	}

	return z
}

Sample output

Z Val [ 327 ] , Predicted [ 106929 ], Actual [ 653 ]
Z Val [ 164.49847094801223 ] , Predicted [ 27059.746944234023 ], Actual [ 653 ]
Z Val [ 84.23405635482261 ] , Predicted [ 7095.376249987432 ], Actual [ 653 ]
Z Val [ 45.99313261935662 ] , Predicted [ 2115.3682481417263 ], Actual [ 653 ]
Z Val [ 30.095452195580968 ] , Predicted [ 905.7362428564993 ], Actual [ 653 ]
Z Val [ 25.896541323366037 ] , Predicted [ 670.6308525128048 ], Actual [ 653 ]
Z Val [ 25.556131917093378 ] , Predicted [ 653.1158785638788 ], Actual [ 653 ]
Z Val [ 25.553864778931494 ] , Predicted [ 653.0000051399155 ], Actual [ 653 ]
Fell within threshold of [ 0.1 ] within [ 7 ] counts

@igagankalra
Copy link
Copy Markdown

igagankalra commented Jul 4, 2020

package main

import (
	"fmt"
	"math"
)

func Sqrt(x float64) float64 {
	z := 1.0
	for x >= z {
		z -= (z*z - x) / (2 * z)
		fmt.Println(z)
		if z == math.Sqrt(x) {
			break
		}

	}
	return z

}

func main() {
	fmt.Println(Sqrt(9999800001))

	fmt.Println("printing")

	fmt.Println(math.Sqrt(9999800001))

}

@horizon365
Copy link
Copy Markdown

horizon365 commented Dec 9, 2020


import "fmt"

func Sqrt(x float64) float64{
	z := float64(1)
	mydict := make(map[float64]float64)
	for  {
		z -= (z*z - x) / (x * z)
		_, ok := mydict[z]
		if !ok{
			mydict[z] = z
		} else {
			break
		}
		fmt.Println(z)
	}
	return z
}

func main() {
	fmt.Println(Sqrt(3))
}

@tiomno
Copy link
Copy Markdown

tiomno commented Feb 1, 2022

package main

import (
	"fmt"
)

func Sqrt(x float64) float64 {
	z := 1.0

	for {
		distance := z*z - x
		
		if distance > -.000000001 && distance < ..000000001 {
			break
		}
		
		z -= distance / (2*z)
		
		fmt.Println(z)
	}

	return z
}

func main() {
	fmt.Println(Sqrt(2))
}

@bboy114crew
Copy link
Copy Markdown

bboy114crew commented Mar 20, 2022

package main

import (
	"fmt"
	"math"
)

const epison = 0.00001

func Sqrt(x float64) float64 {
	result := 1.
	for {
		result = result + (x / (2 * result) - result / 2)
		if math.Abs(result * result - x) < epison {
			break
		}
	}
	return result
}

func main() {
	fmt.Println(Sqrt(2))
}

@keshabagadia
Copy link
Copy Markdown

package main
import (
"fmt"
"math"
)

func Sqrt(x float64) float64 {
z,t := 1.,0.

for math.Abs(t-z) >= 1e-8 {
    t,z = z, z - (z*z - x) / (2*z)
}
return z

}
func main() {
guess := Sqrt(2)
expected := math.Sqrt(2)
fmt.Printf("Guess: %v, Expected: %v, Error: %v", guess, expected, math.Abs(guess - expected))
}

@thaiquangquy
Copy link
Copy Markdown

Can anyone let me know the different of z -= (z*z - x) / (2 * x) and z -= (z * z - x)/(2 * z) in attached example?
The result is different between these expressions which make me very confuse

You can find the full code here https://go.dev/play/p/ff8LzDMIg8c

@gozilla-paradise
Copy link
Copy Markdown

gozilla-paradise commented Dec 13, 2022

package main

import (
	"fmt"
	"math"
)

func Sqrt(x float64) float64 {
	z,p := 1.0, 0.0
	for math.Abs(p-z) >= 1e-8 {
		p,z = z, z - (z*z - x) / (2*z)
	}
	return z
}

func main() {
	fmt.Println(Sqrt(2))
}

@nikurasutan
Copy link
Copy Markdown

package main

import (
	"fmt"
	"math"
)

func Sqrt(x float64) (z float64, iterations int) {
	z = 1.0
	iterations = 0
	for {
		iterations++
		temp := z - (z*z - x) / (2*z)
		if math.Round(temp * 100000) / 100000 == math.Round(z * 100000) / 100000 {
			break
		}
		z = temp
	}
	return
}

func main() {
	fmt.Println(Sqrt(2))
	fmt.Println(math.Sqrt(2))
}

As a total beginner I've done it with rounding

@maricarmendev
Copy link
Copy Markdown

package main

import (
	"fmt"
)

func Sqrt(x float64) float64 {
	z := 1.0
	for i := 0; i <= 10; i++ {
		z -= (z*z - x) / (2 * z)
	}
	return z
}

func main() {
	fmt.Println(Sqrt(23123))
}

@ankitkumar5422
Copy link
Copy Markdown

package main

import (
"fmt"
"math"
)

func Sqrt(x float64) float64 {
z:=1.0

for n:= 1;n < 10;n++ {
	z = z - ((z*z - x) / (2*z))
}
return z

}

func main() {
y := 169.
fmt.Println(Sqrt(y))
fmt.Println(math.Sqrt(y))
}

@vishwa5854
Copy link
Copy Markdown

func Sqrt(x float64) float64 {
var z, tmp float64 = 1.0, 0.
for ; math.Abs(z - tmp) >= 1e-8 ; z, tmp = z - (zz-x)/(2z), z {}
return z
}

@masiunas
Copy link
Copy Markdown

package main

import (
"fmt"
)

func Sqrt(x float64) float64 {
z := float64(1)
for i := 1; i <= 10; i++ {
nextZ := z - (zz-x)/(2z)
if nextZ == z {
return z
} else {
z = nextZ
fmt.Println(z, i)
}
}
return z
}

func main() {
p := 2.0
fmt.Println(Sqrt(p))
}

@NATSUNOAME1337
Copy link
Copy Markdown

package main

import (
"fmt"
"math"
)

func Sqrt(x float64) float64 {
z := 1.0
iterations := 0

for i := 0; i <= 100; i++ {

	z -= (z*z - x) / (2 * z)
	iterations += 1

	if z == math.Sqrt(x) {
		break
	}
}
fmt.Println(z, "iterations: ", iterations)
return z

}

func main() {
var x float64 = 102444
fmt.Println("Guess: ", Sqrt(x))
fmt.Println("Math module: ", math.Sqrt(x))
}

@filipcvejic
Copy link
Copy Markdown

filipcvejic commented Apr 29, 2025

package main

import (
	"fmt"
	"math"
)

func Sqrt(x float64) float64 {
	prev, z := 0.0, 1.0
	
	for math.Abs(prev - z) > 1e-3 {
		prev = z
		z -= (z*z - x) / (2*z)
	}
	
	return z
}

func main() {
	n := 2.0
	g := Sqrt(n)
	a := math.Sqrt(n)
	d := math.Abs(g - a)
	fmt.Printf("Guessed: %f  Actual: %f  Delta: %f\n", g, a, d)
}

@ArturSultanov
Copy link
Copy Markdown

ArturSultanov commented Sep 15, 2025

package main

import (
	"fmt";
	"math"
)

func Sqrt(x float64) float64 {
	z := 1.0
	for math.Abs(z*z-x) > 1e-8 {
		z -= (z*z - x) / (2*z)
	}
	return z
}

func main() {
	x := float64(2)
	fmt.Println(Sqrt(x))
}

@gmyrianthous
Copy link
Copy Markdown

gmyrianthous commented Oct 26, 2025

package main

import (
	"fmt"
	"math"
)

func Sqrt(x float64) float64 {
	z := 1.0
	
	for math.Abs(z * z - x) > 1e-15 {
		z -= (z * z - x) / (2 * z)
	}
	
	return z
}

func main() {
	fmt.Printf("Own implementation result:\t %v\n", Sqrt(2))
	fmt.Printf("math package result:\t\t %v", math.Sqrt(2))
}

@ayclqt
Copy link
Copy Markdown

ayclqt commented Dec 16, 2025

package main

import (
	"fmt"
)

func Sqrt(x float64) float64 {
	z := 1.
	for i:=0; i<10; i++ { // for 10 iterations
		z -= (z*z - x) / (2*z)
		if z*z == x {
			return z
		}
	}
	return z
}

func main() {
	fmt.Println(Sqrt(2))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment