Count quadruplets ( A , B , C , D ) till N such that sum of square of A and B is equal to that of C and D | Function to count the quadruples ; Counter variable ; Map to store the sum of pair ( a ^ 2 + b ^ 2 ) ; Iterate till N ; Calculate a ^ 2 + b ^ 2 ; Increment the value in map ; Check if this sum was also in a ^ 2 + b ^ 2 ; Return the count ; Given N ; Function Call
function countQuadraples ( N ) { var cnt = 0 ; var m = new Map ( ) ; for ( var a = 1 ; a <= N ; a ++ ) { for ( var b = 1 ; b <= N ; b ++ ) { var x = a * a + b * b ; if ( m . has ( x ) ) m . set ( x , m . get ( x ) + 1 ) else m . set ( x , 1 ) } } for ( var c = 1 ; c <= N ; c ++ ) { for ( var d = 1 ; d <= N ; d ++ ) { var x = c * c + d * d ; if ( m . has ( x ) ) cnt += m . get ( x ) ; } } return cnt ; } var N = 2 ; document . write ( countQuadraples ( N ) )