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 | Java program for the above approach ; 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 ; Driver code ; Given N ; Function call
import java . util . * ; class GFG { static long countQuadraples ( long N ) { long cnt = 0 ; Map < Long , Long > m = new HashMap < > ( ) ; for ( long a = 1 ; a <= N ; a ++ ) { for ( long b = 1 ; b <= N ; b ++ ) { long x = a * a + b * b ; m . put ( x , m . getOrDefault ( x , 0l ) + 1 ) ; } } for ( long c = 1 ; c <= N ; c ++ ) { for ( long d = 1 ; d <= N ; d ++ ) { long x = c * c + d * d ; if ( m . containsKey ( x ) ) cnt += m . get ( x ) ; } } return cnt ; } public static void main ( String [ ] args ) { long N = 2 ; System . out . println ( countQuadraples ( N ) ) ; } }