Count number of triangles possible with length of sides not exceeding N | C ++ implementation of the above approach ; 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 ; Insert the three sides in the set to find unique triangles ; return the size of set ; Driver code ; Given N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int right_angled ( int n ) { set < pair < int , pair < int , int > > > s ; for ( int x = 1 ; x <= n ; x ++ ) { for ( int y = 1 ; y <= n ; y ++ ) { if ( x * x + y * y <= n * n ) { int z = sqrt ( x * x + y * y ) ; if ( z * z != ( x * x + y * y ) ) continue ; vector < int > v ; v . push_back ( x ) ; v . push_back ( y ) ; v . push_back ( sqrt ( x * x + y * y ) ) ; sort ( v . begin ( ) , v . end ( ) ) ; s . insert ( { v [ 0 ] , { v [ 1 ] , v [ 2 ] } } ) ; } else break ; } } return s . size ( ) ; } int main ( ) { int n = 5 ; cout << right_angled ( n ) ; return 0 ; }