Count number of right triangles possible with a given perimeter | C ++ program to find the number of right triangles with given perimeter ; Function to return the count ; making a list to store ( a , b ) pairs ; no triangle if p is odd ; make ( a , b ) pair in sorted order ; check to avoid duplicates ; store the new pair ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTriangles ( int p ) { vector < pair < int , int > > store ; if ( p % 2 != 0 ) return 0 ; else { int count = 1 ; for ( int b = 1 ; b < p / 2 ; b ++ ) { float a = ( float ) p / 2.0f * ( ( float ) ( ( float ) p - 2.0 * ( float ) b ) / ( ( float ) p - ( float ) b ) ) ; int inta = ( int ) ( a ) ; if ( a == inta ) { pair < int , int > ab ; if ( inta < b ) { ab = { inta , b } ; } else { ab = { b , inta } ; } if ( find ( store . begin ( ) , store . end ( ) , ab ) == store . end ( ) ) { count += 1 ; store . push_back ( ab ) ; } } } return count ; } } int main ( ) { int p = 840 ; cout << " number ▁ of ▁ right ▁ triangles ▁ = ▁ " << countTriangles ( p ) ; return 0 ; }