Square root of two Complex Numbers - GeeksforGeeks

14 Jun, 2021
Given two positive integers A and B representing the complex number Z in the form of Z = A + i * B, the task is to find the square root of the given complex number.
Examples:
Input: A = 0, B =1Output:The Square roots are: 0.707107 + 0.707107*i-0.707107 – 0.707107*i
Input: A = 4, B = 0Output:The Square roots are: 2-2
Approach: The given problem can be solved based on the following observations:
It is known that the square root of a complex number is also a complex number.
Then considering the square root of the complex number equal to X + i*Y, the value of (A + i*B) can be expressed as:A + i * B = (X + i * Y) * (X + i * Y)A + i * B = X2 – Y2+ 2 * i * X * Y
A + i * B = (X + i * Y) * (X + i * Y)
A + i * B = X2 – Y2+ 2 * i * X * Y
Equating the value of real and complex parts individually:
From the above observations, calculate the value of X and Y using the above formula and print the value (X + i*Y) as the resultant square root value of the given complex number.
Below is the implementation of the above approach:
C++
Java
Python3
Javascript
// C++ program for the above approach #include <bits/stdc++.h>using namespace std; // Function to find the square root of// a complex numbervoid complexRoot(int A, int B){    // Stores all the square roots    vector<pair<double, double> > ans;     // Stores the first square root    double X1 = abs(sqrt((A + sqrt(A * A                                   + B * B))                         / 2));    double Y1 = B / (2 * X1);     // Push the square root in the ans    ans.push_back({ X1, Y1 });     // Stores the second square root    double X2 = -1 * X1;    double Y2 = B / (2 * X2);     // If X2 is not 0    if (X2 != 0) {         // Push the square root in        // the array ans[]        ans.push_back({ X2, Y2 });    }     // Stores the third square root    double X3 = (A - sqrt(A * A + B * B)) / 2;     // If X3 is greater than 0    if (X3 > 0) {        X3 = abs(sqrt(X3));        double Y3 = B / (2 * X3);         // Push the square root in        // the array ans[]        ans.push_back({ X3, Y3 });         // Stores the fourth square root        double X4 = -1 * X3;        double Y4 = B / (2 * X4);         if (X4 != 0) {             // Push the square root            // in the array ans[]            ans.push_back({ X4, Y4 });        }    }     // Prints the square roots    cout << "The Square roots are: "         << endl;     for (auto p : ans) {        cout << p.first;        if (p.second > 0)            cout << "+";        if (p.second)            cout << p.second                 << "*i" << endl;        else            cout << endl;    }} // Driver Codeint main(){    int A = 0, B = 1;    complexRoot(A, B);     return 0;}
// Java program for the above approachimport java.util.*; class GFG{     static class pair{    double first, second;    public pair(double first,                double second)     {        this.first = first;        this.second = second;    }   } // Function to find the square root of// a complex numberstatic void complexRoot(int A, int B){         // Stores all the square roots    Vector<pair> ans = new Vector<pair>();     // Stores the first square root    double X1 = Math.abs(Math.sqrt((A + Math.sqrt(A * A +                                    B * B)) / 2));    double Y1 = B / (2 * X1);     // Push the square root in the ans    ans.add(new pair( X1, Y1 ));     // Stores the second square root    double X2 = -1 * X1;    double Y2 = B / (2 * X2);     // If X2 is not 0    if (X2 != 0)    {                 // Push the square root in        // the array ans[]        ans.add(new pair(X2, Y2));    }     // Stores the third square root    double X3 = (A - Math.sqrt(A * A + B * B)) / 2;     // If X3 is greater than 0    if (X3 > 0)    {        X3 = Math.abs(Math.sqrt(X3));        double Y3 = B / (2 * X3);         // Push the square root in        // the array ans[]        ans.add(new pair(X3, Y3));         // Stores the fourth square root        double X4 = -1 * X3;        double Y4 = B / (2 * X4);         if (X4 != 0)        {                         // Push the square root            // in the array ans[]            ans.add(new pair(X4, Y4));        }    }     // Prints the square roots    System.out.print("The Square roots are: " + "\n");     for(pair p : ans)    {        System.out.printf("%.4f", p.first);        if (p.second > 0)            System.out.print("+");        if (p.second != 0)            System.out.printf("%.4f*i\n", p.second);        else            System.out.println();    }} // Driver Codepublic static void main(String[] args){    int A = 0, B = 1;         complexRoot(A, B);}} // This code is contributed by shikhasingrajput
# Python3 program for the above approachfrom math import sqrt # Function to find the square root of# a complex numberdef complexRoot(A, B):         # Stores all the square roots    ans = []     # Stores the first square root    X1 = abs(sqrt((A + sqrt(A * A + B * B)) / 2))    Y1 = B / (2 * X1)     # Push the square root in the ans    ans.append([X1, Y1])     # Stores the second square root    X2 = -1 * X1    Y2 = B / (2 * X2)     # If X2 is not 0    if (X2 != 0):         # Push the square root in        # the array ans[]        ans.append([X2, Y2])     # Stores the third square root    X3 = (A - sqrt(A * A + B * B)) / 2     # If X3 is greater than 0    if (X3 > 0):        X3 = abs(sqrt(X3))        Y3 = B / (2 * X3)         # Push the square root in        # the array ans[]        ans.append([X3, Y3])         # Stores the fourth square root        X4 = -1 * X3        Y4 = B / (2 * X4)         if (X4 != 0):             # Push the square root            # in the array ans[]            ans.append([X4, Y4])     # Prints the square roots    print("The Square roots are: ")     for p in ans:        print(round(p[0], 6), end = "")        if (p[1] > 0):            print("+", end = "")        if (p[1]):            print(str(round(p[1], 6)) + "*i")        else:            print() # Driver Codeif __name__ == '__main__':         A,B = 0, 1    complexRoot(A, B) # This code is contributed by mohit kumar 29
<script> // Javascript program for the above approach // Function to find the square root of// a complex numberfunction complexRoot(A, B){    // Stores all the square roots    var ans = [];     // Stores the first square root    var X1 = Math.abs(Math.sqrt((A + Math.sqrt(A * A                                   + B * B))                         / 2));    var Y1 = B / (2 * X1);     // Push the square root in the ans    ans.push([X1, Y1]);     // Stores the second square root    var X2 = -1 * X1;    var Y2 = B / (2 * X2);     // If X2 is not 0    if (X2 != 0) {         // Push the square root in        // the array ans[]        ans.push([X2, Y2]);    }     // Stores the third square root    var X3 = (A - Math.sqrt(A * A + B * B)) / 2;     // If X3 is greater than 0    if (X3 > 0) {        X3 = Math.abs(Math.sqrt(X3));        var Y3 = B / (2 * X3);         // Push the square root in        // the array ans[]        ans.push([X3, Y3]);         // Stores the fourth square root        var X4 = -1 * X3;        var Y4 = B / (2 * X4);         if (X4 != 0) {             // Push the square root            // in the array ans[]            ans.push([X4, Y4]);        }    }     // Prints the square roots    document.write( "The Square roots are: <br>");     ans.forEach(p => {        document.write( p[0].toFixed(6));        if (p[1] > 0)            document.write( "+");        if (p[1])            document.write( p[1].toFixed(6)                 + "*i<br>" );        else            document.write("<br>");    });} // Driver Codevar A = 0, B = 1;complexRoot(A, B); </script>
The Square roots are: 
0.707107+0.707107*i
-0.707107-0.707107*i
 
Time Complexity: O(1)Auxiliary Space: O(1)
mohit kumar 29
shikhasingrajput
noob2000
Numbers
Mathematical
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Modular multiplicative inverse
Fizz Buzz Implementation
Generate all permutation of a set in Python
How to check if a given point lies inside or outside a polygon?
Check if a number is Palindrome
Segment Tree | Set 1 (Sum of given range)
Merge two sorted arrays with O(1) extra space
Program to multiply two matrices
Singular Value Decomposition (SVD)