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
function checkTriangularSumRepresentation ( n ) { var tri = new Set ( ) ; var i = 1 ; while ( 1 ) { var x = i * parseInt ( ( i + 1 ) / 2 ) ; if ( x >= n ) break ; tri . add ( x ) ; i ++ ; } var ans = false ; tri . forEach ( tm => { if ( tri . has ( n - tm ) ) ans = true } ) ; return ans ; } var n = 24 ; checkTriangularSumRepresentation ( n ) ? document . write ( " " ) : document . write ( " " ) ;