Square root of two Complex Numbers | Python3 program for the above approach ; Function to find the square root of a complex number ; Stores all the square roots ; Stores the first square root ; Push the square root in the ans ; Stores the second square root ; If X2 is not 0 ; Push the square root in the array ans [ ] ; Stores the third square root ; If X3 is greater than 0 ; Push the square root in the array ans [ ] ; Stores the fourth square root ; Push the square root in the array ans [ ] ; Prints the square roots ; Driver Code
from math import sqrt NEW_LINE def complexRoot ( A , B ) : NEW_LINE INDENT ans = [ ] NEW_LINE X1 = abs ( sqrt ( ( A + sqrt ( A * A + B * B ) ) / 2 ) ) NEW_LINE Y1 = B / ( 2 * X1 ) NEW_LINE ans . append ( [ X1 , Y1 ] ) NEW_LINE X2 = - 1 * X1 NEW_LINE Y2 = B / ( 2 * X2 ) NEW_LINE if ( X2 != 0 ) : NEW_LINE INDENT ans . append ( [ X2 , Y2 ] ) NEW_LINE DEDENT X3 = ( A - sqrt ( A * A + B * B ) ) / 2 NEW_LINE if ( X3 > 0 ) : NEW_LINE INDENT X3 = abs ( sqrt ( X3 ) ) NEW_LINE Y3 = B / ( 2 * X3 ) NEW_LINE ans . append ( [ X3 , Y3 ] ) NEW_LINE X4 = - 1 * X3 NEW_LINE Y4 = B / ( 2 * X4 ) NEW_LINE if ( X4 != 0 ) : NEW_LINE INDENT ans . append ( [ X4 , Y4 ] ) NEW_LINE DEDENT DEDENT print ( " The ▁ Square ▁ roots ▁ are : ▁ " ) NEW_LINE for p in ans : NEW_LINE INDENT print ( round ( p [ 0 ] , 6 ) , end = " " ) NEW_LINE if ( p [ 1 ] > 0 ) : NEW_LINE INDENT print ( " + " , end = " " ) NEW_LINE DEDENT if ( p [ 1 ] ) : NEW_LINE INDENT print ( str ( round ( p [ 1 ] , 6 ) ) + " * i " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A , B = 0 , 1 NEW_LINE complexRoot ( A , B ) NEW_LINE DEDENT