Count number of right triangles possible with a 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
def countTriangles ( p ) : NEW_LINE INDENT store = [ ] NEW_LINE if p % 2 != 0 : return 0 NEW_LINE else : NEW_LINE INDENT count = 0 NEW_LINE for b in range ( 1 , p // 2 ) : NEW_LINE INDENT a = p / 2 * ( ( p - 2 * b ) / ( p - b ) ) NEW_LINE inta = int ( a ) NEW_LINE if ( a == inta ) : NEW_LINE INDENT ab = tuple ( sorted ( ( inta , b ) ) ) NEW_LINE if ab not in store : NEW_LINE INDENT count += 1 NEW_LINE store . append ( ab ) NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT DEDENT p = 840 NEW_LINE print ( " number ▁ of ▁ right ▁ triangles ▁ = ▁ " + str ( countTriangles ( p ) ) ) NEW_LINE