Find smallest perfect square number A such that N + A is also a perfect square number | JavaScript code to find out the smallest perfect square X which when added to N yields another perfect square number . ; X is the smallest perfect square number ; Loop from 1 to square root of N ; Condition to check whether i is factor of N or not ; Condition to check whether factors satisfies the equation or not ; Stores minimum value ; Return if X * X if X is not equal to 1e9 else return - 1 ; Driver code
function SmallestPerfectSquare ( N ) { let X = 1e9 ; let ans ; for ( let i = 1 ; i < Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { let a = i ; let b = N / i ; if ( ( b - a != 0 ) && ( ( b - a ) % 2 == 0 ) ) { X = Math . min ( X , ( b - a ) / 2 ) ; } } } if ( X != 1e9 ) ans = X * X ; else ans = - 1 ; return ans ; } let N = 3 ; document . write ( SmallestPerfectSquare ( N ) ) ;