Find two numbers such that difference of their squares equal to N | Function to check and print the required two positive integers ; Iterate till sqrt ( n ) to find factors of N ; Check if x is one of the factors of N ; Store the factor ; Compute the other factor ; Check if the two factors are of the same parity ; Compute a and b ; If no pair exists ; Driver Code
function solve ( n ) { for ( var x = 1 ; x <= Math . sqrt ( n ) ; x ++ ) { if ( n % x == 0 ) { var small = x ; var big = n / x ; if ( small % 2 == big % 2 ) { var a = ( small + big ) / 2 ; var b = ( big - small ) / 2 ; document . write ( a + " " + b ) ; return ; } } } document . write ( - 1 ) ; } var n = 7 ; solve ( n ) ;