📜  两个复数的平方根

📅  最后修改于: 2022-05-13 01:56:04.882000             🧑  作者: Mango

两个复数的平方根

给定两个正整数AB ,表示复数Z ,形式为Z = A + i * B ,任务是求给定复数的平方根。

例子:

方法:可以根据以下观察解决给定的问题:

  • 众所周知,复数的平方根也是复数。
  • 然后考虑复数的平方根等于X + i*Y(A + i*B)的值可以表示为:
    • A + i * B = (X + i * Y) * (X + i * Y)
    • A + i * B = X 2 – Y 2 + 2 * i * X * Y
  • 分别使实部和复部的值相等:
    • X = \sqrt (\frac {(A \pm (A^{2} + B^{2}))}{2})
    • Y = \frac{B}{2 \times \sqrt (\frac {(A \pm (A^{2} + B^{2}))}{2})}

根据上述观察,使用上述公式计算XY的值,并将值(X + i*Y)打印为给定复数的结果平方根值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the square root of
// a complex number
void complexRoot(int A, int B)
{
    // Stores all the square roots
    vector > 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 Code
int main()
{
    int A = 0, B = 1;
    complexRoot(A, B);
 
    return 0;
}


Java
// Java program for the above approach
import 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 number
static void complexRoot(int A, int B)
{
     
    // Stores all the square roots
    Vector ans = new Vector();
 
    // 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 Code
public static void main(String[] args)
{
    int A = 0, B = 1;
     
    complexRoot(A, B);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
from math import sqrt
 
# Function to find the square root of
# a complex number
def 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 Code
if __name__ == '__main__':
     
    A,B = 0, 1
    complexRoot(A, B)
 
# This code is contributed by mohit kumar 29


Javascript


输出:
The Square roots are: 
0.707107+0.707107*i
-0.707107-0.707107*i

时间复杂度: O(1)
辅助空间: O(1)