Skip to content

Instantly share code, notes, and snippets.

@stamler
Created August 6, 2013 19:01
Show Gist options
  • Save stamler/6167545 to your computer and use it in GitHub Desktop.
Save stamler/6167545 to your computer and use it in GitHub Desktop.
Exercise 47 (Complex Cube Root) from tour.golang.org
package main
import "fmt"
import "math/cmplx"
// #47, Complex cube roots
// Cbrt returns a complex128 that is the cube
// root of its lone argument
func Cbrt(x complex128) complex128 {
z := 1.0 + 0i
for i := 0; i < 20; i++ {
z -= (cmplx.Pow(z, 3) - x) / (3 * z * z)
}
return z
}
func main() {
fmt.Println(Cbrt(2))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment