diff --git a/exercises/04-cube/go.go b/exercises/04-cube/go.go new file mode 100644 index 0000000..33d0060 --- /dev/null +++ b/exercises/04-cube/go.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "math/cmplx" +) + +const threshold = 1e-5 + +func Cbrt(x complex128) (z complex128) { + z = x + for delta, last := 1e9, z; delta > threshold; last = z { + z -= (z*z*z - x) / (3 * z * z) + delta = cmplx.Abs(z - last) + } + return +} + +func main() { + n := 12392.13 + 1982i + fmt.Println("calculated ", Cbrt(n)) + fmt.Println("expected ", cmplx.Pow(n, 1/3.0)) +}