Finding the Square Root of the Complex Number in Golang


In mathematics, a square root of a number is a value that when multiplied by itself, gives the original number. In Golang, the math/cmplx package provides built-in functions to find the square root of a complex number. In this article, we will discuss how to find the square root of a complex number in Golang with examples.

Example 1: Finding the Square Root of a Complex Number

Let's consider an example of finding the square root of a complex number using Golang. Suppose we want to find the square root of z = 3 + 4i. Here's the code snippet to accomplish this −

package main

import (
   "fmt"
   "math/cmplx"
)

func main() {
   // Creating a complex number
   z := complex(3, 4)
   
   // Finding the square root of the complex number
   sqrtZ := cmplx.Sqrt(z)
   
   // Displaying the result
   fmt.Println("Square Root of", z, "is", sqrtZ)
}

Output

Square Root of (3+4i) is (2+1i)

In this example, we first create a complex number z and then use the cmplx.Sqrt() function to find its square root and store the result in sqrtZ. The output of this program will be −

Example 2: Finding the Square Root of a Purely Imaginary Number

Let's consider an example of finding the square root of a purely imaginary number using Golang. Suppose we want to find the square root of y = 2i. Here's the code snippet to accomplish this −

package main

import (
   "fmt"
   "math/cmplx"
)

func main() {
   // Creating a purely imaginary number
   y := 2i
   
   // Finding the square root of the purely imaginary number
   sqrtY := cmplx.Sqrt(y)
   
   // Displaying the result
   fmt.Println("Square Root of", y, "is", sqrtY)
}

Output

Square Root of (0+2i) is (1+1i)

In this example, we first created a purely imaginary number y and then used the cmplx.Sqrt() function to find its square root and store the result in sqrtY.

Example 3: Finding the Square Root of a Negative Real Number

Let's consider an example of finding the square root of a negative real number using Golang. Suppose we want to find the square root of -4. Here's the code snippet to accomplish this −

package main

import (
   "fmt"
   "math/cmplx"
)

func main() {
   // Creating a negative real number
   z := -4.0
   
   // Finding the square root of the negative real number
   sqrtZ := cmplx.Sqrt(complex(z, 0))
   
   // Displaying the result
   fmt.Println("Square Root of", z, "is", sqrtZ)
}

Output

Square Root of -4 is (0+2i)

In this example, we first created a negative real number z and then used the cmplx.Sqrt() function to find its square root and store the result in sqrtZ. Since the square root of a negative real number is a complex number, we need to pass z as a complex number with an imaginary part of 0.

Example 4: Finding Multiple Square Roots of a Complex Number

In Golang, we can find the multiple square roots of a complex number. For a given complex number z = x + yi, we can find the square roots using the formula −

sqrt(z) = +/- sqrt(r) * [cos((theta + 2k*pi)/2) + i*sin((theta + 2k*pi)/2)], k = 0, 1

Where r = |z| is the modulus of the complex number z, and theta = arg(z) is the argument of the complex number z.

Let's consider an example where we want to find the two square roots of a complex number z = 3 + 4i. Here's the code snippet to accomplish this −

package main

import (
   "fmt"
   "math/cmplx"
)
   
func main() {
   // Creating a complex number
   z := complex(3, 4)
   
   // Finding the square roots of the complex number
   sqrt1 := cmplx.Sqrt(z)
   sqrt2 := -cmplx.Sqrt(z)
   
   // Displaying the result
   fmt.Printf("Square Roots of %v are:\n%v\n%v", z, sqrt1, sqrt2)
}

Output

Square Roots of (3+4i) are:
(2+1i)
(-2-1i)

Conclusion

Finding the square root of a complex number in Golang is straightforward with the help of the cmplx.Sqrt() function. By using this function, we can easily calculate the square root of any complex number, whether it is purely real or imaginary, or a combination of both. Additionally, we can use the cmplx.Pow() function to find any nth root of a complex number. It's important to note that when taking roots of complex numbers, there can be multiple solutions, so we need to carefully choose the one that fits our use case. With the knowledge and examples provided in this article, you should be able to use Golang to efficiently compute the square roots of complex numbers in your projects.

Updated on: 17-Apr-2023

184 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements