Check if a number can be represented as a sum of 2 triangular numbers | Function to check if it is possible or not ; Store all triangular numbers up to N in a Set ; Check if a pair exists ; Driver Code
< ? php function checkTriangularSumRepresentation ( $ n ) { $ tri = array ( ) ; $ i = 1 ; while ( true ) { $ x = $ i * ( $ i + 1 ) ; if ( $ x >= $ n ) break ; array_push ( $ tri , $ x ) ; $ i += 1 ; } foreach ( $ tri as $ tm ) if ( in_array ( $ n - $ tm , $ tri ) ) return true ; return false ; } $ n = 24 ; if ( checkTriangularSumRepresentation ( $ n ) ) print ( " Yes " ) ; else print ( " No " ) ; ? >