Skip to content

Instantly share code, notes, and snippets.

@tetsuok
Created April 2, 2012 01:58
Show Gist options
  • Save tetsuok/2279991 to your computer and use it in GitHub Desktop.
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)
}
@tiomno
Copy link

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

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

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

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

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

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

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

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

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

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))
}

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