Count number of triangles possible with length of sides not exceeding N | Java implementation of the above approach ; First member of pair ; Second member of pair ; Function to count total number of right angled triangle ; Consider a set to store the three sides ; Find possible third side ; Condition for a right angled triangle ; Check if the third side is an integer ; Push the three sides ; Add the three sides in the set to find unique triangles ; Return the size of set ; Driver code ; Given N ; Function call
import java . util . * ; class Pair < F , S > { private F first ; private S second ; public Pair ( F first , S second ) { this . first = first ; this . second = second ; } } class GFG { public static int right_angled ( int n ) { Set < Pair < Integer , Pair < Integer , Integer > > > s = new HashSet < Pair < Integer , Pair < Integer , Integer > > > ( ) ; for ( int x = 1 ; x <= n ; x ++ ) { for ( int y = 1 ; y <= n ; y ++ ) { if ( x * x + y * y <= n * n ) { int z = ( int ) Math . sqrt ( x * x + y * y ) ; if ( z * z != ( x * x + y * y ) ) continue ; Vector < Integer > v = new Vector < Integer > ( ) ; v . add ( x ) ; v . add ( y ) ; v . add ( ( int ) Math . sqrt ( x * x + y * y ) ) ; Collections . sort ( v ) ; s . add ( new Pair < Integer , Pair < Integer , Integer > > ( v . get ( 0 ) , new Pair < Integer , Integer > ( v . get ( 1 ) , v . get ( 2 ) ) ) ) ; } else break ; } } return s . size ( ) - 1 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( right_angled ( n ) ) ; } }