Count of pair of integers ( x , y ) such that difference between square of x and y is a perfect square | Function to find number of pairs ( x , y ) such that x ^ 2 - y is a square number ; Stores the count of total pairs ; Iterate q value 1 to sqrt ( N ) ; Maximum possible value of p is min ( 2 * N - q , N / q ) ; P must be greater than or equal to q ; Total number of pairs are ; Adding all valid pairs to res ; Return total no of pairs ( x , y ) ; Driver Code
function countPairs ( N ) { let res = 0 ; for ( let q = 1 ; q * q <= N ; q ++ ) { let maxP = Math . min ( 2 * N - q , N / q ) ; if ( maxP < q ) continue ; let cnt = maxP - q + 1 ; res += Math . floor ( cnt / 2 + ( cnt & 1 ) ) ; } return res ; } let N = 3 ; document . write ( countPairs ( N ) ) ;