Square root of two Complex Numbers | 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
function complexRoot ( A , B ) { var ans = [ ] ; var X1 = Math . abs ( Math . sqrt ( ( A + Math . sqrt ( A * A + B * B ) ) / 2 ) ) ; var Y1 = B / ( 2 * X1 ) ; ans . push ( [ X1 , Y1 ] ) ; var X2 = - 1 * X1 ; var Y2 = B / ( 2 * X2 ) ; if ( X2 != 0 ) { ans . push ( [ X2 , Y2 ] ) ; } var X3 = ( A - Math . sqrt ( A * A + B * B ) ) / 2 ; if ( X3 > 0 ) { X3 = Math . abs ( Math . sqrt ( X3 ) ) ; var Y3 = B / ( 2 * X3 ) ; ans . push ( [ X3 , Y3 ] ) ; var X4 = - 1 * X3 ; var Y4 = B / ( 2 * X4 ) ; if ( X4 != 0 ) { ans . push ( [ X4 , Y4 ] ) ; } } document . write ( " " ) ; ans . forEach ( p => { document . write ( p [ 0 ] . toFixed ( 6 ) ) ; if ( p [ 1 ] > 0 ) document . write ( " " ) ; if ( p [ 1 ] ) document . write ( p [ 1 ] . toFixed ( 6 ) + " " ) ; else document . write ( " " ) ; } ) ; } var A = 0 , B = 1 ; complexRoot ( A , B ) ;